{$V-}
UNIT WTGSubs;
{ *******************
   MODIFS DELPHI1
********************* }

{***************************************************************}
{*                                                             *}
{*   Copyright (c) Quinn-Curtis, 1992                          *}
{*                                                             *}
{*   Filename:  RTGSUBS.PAS                                    *}
{*   Revision:  3.0                                            *}
{*   Date:      2/3/92                                        *}
{*                                                             *}
{*   Description: -Routines for R-T graphics                   *}
{*                -Contains all window support routines        *}
{*                -Axis drawing and labeling, grid line drawing*}
{*                -Simple plotting routines                    *}
{*                -Contains subroutines common to              *}
{*                    rtgraph                                  *}
{*                    rtchart                                  *}
{*                    rtbargr                                  *}
{*                    rttext                                   *}
{*                    rtmeter                                  *}
{*                    rtscreen                                 *}
{*                    rtmouse                                  *}
{*                                                             *}
{***************************************************************}

INTERFACE
USES
   Objects, WinProcs, WinTypes, Strings,
   CartoCmds, tit_win, wtstdhdr, wtarray;

CONST
       maxRTBufferLength = 2000;
       _mrtt = 16;

TYPE
  WorldRect = RECORD
    left, bottom, right, top : REALTYPE;
  END;
  Rect = RECORD
    left, bottom, right, top : INTEGER;
  END;

  rtp2d = RECORD x,y : INTEGER; END;
  rtw2d = RECORD x,y : REALTYPE; END;
  Poly2DType = ARRAY[0..maxRTBufferLength] OF rtp2d;
  RTMarkerType = ARRAY[0..10] OF REALTYPE;
  tagType = string[20];
  titleType = string[40];
  alarmStrType = string[20];
  messagetype = string[40];
  TagArrayType = ARRAY[0.._mrtt-1] OF tagType;
  MessageArrayType = ARRAY[0.._mrtt-1] OF messageType;
  AlarmStrArrayType = ARRAY[0..2] OF alarmStrType;
  RTtimeType = ARRAY[0..3] OF WORD;
  RTBooleanArray = ARRAY[0..10] OF BOOLEAN;
  RTIntArrayType = ARRAY[0.._mrtt-1] OF INTEGER;
  RTBufferArrayType = ARRAY[0..maxRTbufferLength-1] OF rtp2d;
  RTWBufferArrayType = ARRAY[0..maxRTbufferLength-1] OF rtw2d;
  RTBufferType = ARRAY[0.._mrtt-1] OF ^RTBufferArrayType;
  RTWBufferType = ARRAY[0.._mrtt-1] OF ^RTWBufferArrayType;
  RTValueArrayType = ARRAY[0.._mrtt-1] OF REALTYPE;

RTTexttype =   RECORD
  lowalarmlimits,highalarmlimits,setpoints: RTValueArrayType;
  HighalarmStrs, LowalarmStrs, NoalarmStrs: tagArrayType;
  eunitstrs : tagArrayType;
  messageStrs: messageArrayType;
  FlagArray: RTBooleanARRAY;
  lsdelta: INTEGER;
END;


RTGTexttype =   RECORD
  messageStrs: messageArrayType;
  cellW, cellH,columns, rows: INTEGER;
END;


RTGraphType = (notRT, RTScroll, RTSweep, RTLogic, RTXYG,RTScrollBar,
	    RTVBG, RTHBG,RTPCBG,
            RTNMeter, RTClock, RTPMeter,RTAMeter, RTPCText, RTAnnun,
	    RTPanel, RTGText, RTSlider);

rtstattype = RECORD
  valid: BOOLEAN;
  w2pr,pw, pc, mm, nwp : WorldRect;
  pr, dr : Rect;
  numticx, numticy : INTEGER;
  xint, yint, tsx, tsy, ticspacex, ticspacey : REALTYPE;
  pbgcolor, vbgColor, hacolor, lacolor, okcolor,
  labcolor,axiscolor: INTEGER;

  LastValues: RTValueArrayType;
  linecolor,
  linefill: RTIntArrayType;
  titlestr: titleType;
  tagstrs: tagArrayType;
  Eunitstr, EUnitstr2: tagType;
  numTraces: INTEGER;
  xdecimals, ydecimals: INTEGER;
  rtfirstpass : BOOLEAN;
  borderflag, bordercolor,
  printerflag,
  CurFont: INTEGER;
  CurFontSize: INTEGER;
  xscaledir, yscaledir, hvflag, scrolldir: INTEGER;

  CASE RTWinType:RTGraphType OF
    NotRT: (NullField: INTEGER);
    RTGText: (RTGTextStat: RTGTextType);
END; {RECORD}

      rtstatpntr = ^rtstattype;

{ GRAPH simulation }

CONST
  TWO_PI = 2.0 * 3.14159;

  LeftText   =   0;
  BottomText =   0;
  CenterText =   1;
  RightText  =   2;
  TopText    =   2;
  MaxColors = 15;

  DefaultFont   =  0;  {8x8 bit mapped font            }
  TriplexFont   =  1;  {Stroked font                   }
  SmallFont     =  2;  {Stroked font                   }
  SansSerifFont =  3;  {Stroked font                   }
  GothicFont    =  4;  {Stroked font                   }

  HorizDir      =  0;  {Orient left to right           }
  VertDir       =  1;  {Orient bottom to top           }

  UserCharSize  =  0;  {User-defined character size    }

  SolidLn    = 0;                            (* PS_SOLID *)
  DottedLn   = 2;                            (* PS_DOT *)
  CenterLn   = 3;                            (* PS_DASH_DOT *)
  DashedLn   = 1;                            (* PS_DASH *)

  UserBitLn  = 4; {User-defined line style}

  NormWidth  = 1;
  ThickWidth = 3;

  EmptyFill      =  0;  {Uses background color      }
  SolidFill      =  1;  {Uses draw color            }

  LineFill       =  2;  {--- fill                   } (* 0 = hs_horizontal *)
  LtSlashFill    =  3;  {/// fill                   } (* 3 = hs_bdiagonal *)
  SlashFill      =  4;  {/// thick fill             } (* 3 = hs_bdiagonal *)
  BkSlashFill    =  5;  {\\\ thick fill             } (* 2 = hs_fdiagonal *)
  LtBkSlashFill  =  6;  {\\\ fill                   } (* 2 = hs_fdiagonal *)
  HatchFill      =  7;  {Light hatch fill           } (* 4 = hs_cross *)
  XHatchFill     =  8;  {Heavy cross hatch          } (* 4 = hs_cross *)
  InterleaveFill =  9;  {Interleaving line          } (* 5 = hs_diagcross *)
  WideDotFill    = 10;  {Widely spaced dot          } (* 0 = hs_horizontal *)
  CloseDotFill   = 11;  {Closely spaced dot         } (* 0 = hs_horizontal *)

  UserFill       = 12;  {User-defined fill          }

(* Foreground &Background *)

  Black	    = 0;
  Blue	    = 1;
  Green	    = 2;
  Cyan	    = 3;
  Red	    = 4;
  Magenta   = 5;
  Brown	    = 6;
  LightGray = 7;

(* Foreground *)

  DarkGray	= 8;
  LightBlue	= 9;
  LightGreen	=10;
  LightCyan	=11;
  LightRed	=12;
  LightMagenta	=13;
  Yellow	=14;
  White	        =15;

TYPE
  FillSettingsType = record
    Pattern : Word;
    Color   : Word;
  end;

  PaletteType = record
    Size    : Byte;
    Colors  : array[0..MaxColors] of Shortint;
  end;

  ViewPortType = record
    x1, y1, x2, y2 : integer;
    Clip           : Boolean;
  end;

  TextSettingsType = record
    Font      : Word;
    Direction : Word;
    CharSize  : Word;
    Horiz     : Word;
    Vert      : Word;
  end;

TYPE
  PrnSetupType = ARRAY[0..15] OF INTEGER;

CONST
  QC_PRN1= -16;     { QCPRN1.BGI driver - qcprn }
  MaxWin = 64;

VAR      {globals}

   lfill: RTIntArrayType;

TYPE
 PStatPntr = ^TStatPntr;
 TStatPntr = ARRAY[0..MaxWin-1] OF rtstatpntr;

TYPE
  PRT = ^TRT;
  TRT = object(TObject)
               {Globals for windows management }

  rtstat   : PStatPntr;

  rtprnon, gdr, gmd,rtnumwin,ginit,k: INTEGER;
  IntrOffMask,IntrOnMask, IntrMskF : INTEGER;
  maxColor,mx, my, vph, vpl : integer;
  imr, curDir, curSize: word;
  wx, wy, wh, wl,tx,ty : REALTYPE;
  clipFlag: BOOLEAN;
  textHCell, textWCell: INTEGER;

                 { Globals for GRAPH }
  WinPort        : ViewPortType;
  MetaFile       : Boolean;

  OldPen,Pen     : HPen;
  PenColor       : Word;
  PenStyle       : Word;
  PenThick       : Word;

  OldBrush,Brush : hbrush;
  LogBrush       : Tlogbrush;
  BrushColor     : Word;
  BrushFill      : Word;

  OldFont,WinFont: hFont;
  WinText        : TextSettingsType;
  WinLogFont     : TLogFont;
  txtBC, txtFC   : integer;          (* Background, Foreground color text *)
  txtH,txtW      : integer;

                 { Globals for GRAPH }

  WhereX, WhereY : Integer;
  GetMaxX        : Integer;
  GetMaxY        : Integer;
  WinDC          : HDC;
  _indisp        : Integer;
  BasicColors    : ARRAY[0..15] OF TColorRef;
  FillStyles     : ARRAY[0..12] OF integer;

{
procedure getprndrivername(drnum: INTEGER; VAR drstring: STRING);
FUNCTION  scaleprnfont(grmode, font, basesize: INTEGER): INTEGER;
PROCEDURE qcsetupprinter( prnport,orient,formfeed, quietmode: INTEGER;
		          filename: string;VAR   colormap, comportparams,
		          sizeoverrides: PrnSetupType; VAR error: INTEGER);
}

  procedure InitGraph;
  procedure CloseGraph;
  procedure DetectGraph(var GraphDriver, GraphMode: Integer);
  function  GraphResult: Integer;
  function  GraphErrorMsg(ErrorCode: Integer): string;
  function  InstallUserDriver(Name: string; AutoDetectPtr: pointer): Integer;
  function  GetDriverName: string;
  procedure SetWriteMode(WriteMode:Integer);

  procedure GetViewSettings(var ViewPort: ViewPortType);
  procedure SetViewPort(x1, y1, x2, y2: Integer; Clip: Boolean);
  procedure ClearViewPort;

  procedure GetAspectRatio(var Xasp, Yasp: Word);
  procedure GetPalette(var Palette: PaletteType);
  function  GetMaxColor: Word;

  procedure SetFillStyle(Pattern: Word; Color: Word);
  procedure GetFillSettings(var FillInfo:FillSettingsType);
  procedure SetLineStyle(LineStyle: Word; Pattern: Word; Thickness: Word);
  procedure SetColor(Color: Word);
  function  GetColor: Word;
  function  GetX: Integer;
  function  GetY: Integer;
  function  _GetPixel(X, Y: Integer):Word;
  procedure PutPixel(X, Y: Integer; Pixel:Word);

  procedure _MoveTo(X, Y: Integer);
  procedure _MoveRel(Dx, Dy: Integer);
  procedure _LineTo(X, Y: Integer);
  procedure _LineRel(Dx, Dy: Integer);
  procedure Line(x1, y1, x2, y2: Integer);
  procedure _Rectangle(x1, y1, x2, y2: Integer);
  procedure Bar(x1, y1, x2, y2: Integer);
  procedure DrawPoly(NumPoints: Word; var PolyPoints);
  procedure FillPoly(NumPoints: Word; var PolyPoints);
  procedure PieSlice(X, Y: Integer; StAngle,EndAngle, Radius: Word);

  procedure SetTextStyle(Font, Direction: Word; CharSize: Word);
  procedure SetTextJustify(Horiz, Vert: Word);
  procedure GetTextSettings(var TextInfo: TextSettingsType);
  function  TextHeight(TextString: string): Word;
  function  TextWidth(TextString: string): Word;

  procedure OutText(TextString: string);
  function ImageSize(x1, y1, x2, y2:Integer): Word;
  procedure GetImage(x1, y1, x2, y2:Integer; var BitMap);
  procedure PutImage(X, Y: Integer; var BitMap; BitBlt: Word);


{** Available exportable functions **}
FUNCTION  RTGetNumWin: INTEGER;
FUNCTION  RTTH(n: Realtype): INTEGER;
PROCEDURE RTSetIntrMaskFlag(f: INTEGER);
PROCEDURE RTIntrOff;
PROCEDURE RTIntrOn;
FUNCTION  RTInGraphMode: BOOLEAN;
FUNCTION  RTClampReal( r: REALTYPE; l,h : RealType): RealType;
FUNCTION  RTClampRealToInt( r: REALTYPE; l,h : INTEGER): INTEGER;
PROCEDURE RTiswap( VAR x1,x2:  INTEGER);
PROCEDURE RTrswap( VAR xx1,xx2:  REALTYPE);
FUNCTION  RTNumExp( realnum :  REALTYPE ): INTEGER;
PROCEDURE RTFindMinMax(dataset : TPOINTER_C; numdat  :  WORD;
                      VAR minval  :  REALTYPE; VAR maxval  :  REALTYPE);
PROCEDURE RTSortData( VAR x,y : TPOINTER_C;n,d :  INTEGER);
PROCEDURE RTSortDataX( VAR x,y :  TPOINTER_C; n,d :  INTEGER);
PROCEDURE RTSortDataY( VAR x, y:  TPOINTER_C; n, d:  INTEGER );
PROCEDURE RTsetWorldRect( VAR r: worldRect; a,b,c,d : REALTYPE );
PROCEDURE checkoor(VAR ylow, yhigh: REALTYPE; minrange: REALTYPE);
PROCEDURE RTSetMouseIntr(intnum: INTEGER);
FUNCTION  RTViewportWidth: INTEGER;
FUNCTION  RTViewportHeight: INTEGER;
PROCEDURE RTClipOff;
PROCEDURE RTClipOn;
PROCEDURE RTGraphReadln(VAR s: string);
PROCEDURE RTGraphReadReal(VAR r: REALTYPE);
PROCEDURE RTGraphReadInt(VAR i: INTEGER);
PROCEDURE RTRectangleNI(x1,y1,x2,y2: INTEGER);
PROCEDURE RTLineNI(x1,y1,x2,y2: INTEGER);
PROCEDURE RTLineToNI(x1,y1: INTEGER);
PROCEDURE RTLineRelNI(x1,y1: INTEGER);
PROCEDURE RTSetColor(c: INTEGER);
PROCEDURE RTOutTextNI(s:string);
PROCEDURE RTDrawPolyNI(n: word; VAR pnts);

PROCEDURE RTBarNI(x1,y1,x2,y2: INTEGER);
PROCEDURE RTBarXX(x1, y1, x2, y2: INTEGER);
PROCEDURE RTBarXY( xx1, yy1,w,h, oc,gc,gh : integer );
PROCEDURE RTRectangleXX(x1, y1, x2, y2: REALTYPE);
PROCEDURE RTGetMaxCoords(VAR x,y: integer);
PROCEDURE RTLineToXX(x,y: INTEGER);
PROCEDURE RTLineRelXX(x,y: INTEGER);
PROCEDURE RTMoveToXX(x,y: INTEGER);
PROCEDURE RTMoveRelXX(x,y: INTEGER);
PROCEDURE RTGetViewport( VAR xx1, yy1, xx2, yy2 : INTEGER );
PROCEDURE RTSetGraphViewport(xx1,yy1,xx2,yy2: integer);
PROCEDURE RTSetWorldCoordinates(x1,y1,x2,y2 : RealType);
FUNCTION  RTConvertX1( xx1 : REALTYPE ):  INTEGER;
FUNCTION  RTConvertY1(yy1: REALTYPE ) :INTEGER;
FUNCTION  RTConvertX2( xx1: REALTYPE ): INTEGER;
FUNCTION  RTConvertY2( yy1:  REALTYPE ) :INTEGER;
PROCEDURE RTMoveWorldAbs( xx1, yy1 : REALTYPE);
PROCEDURE RTMoveWorldRel(xx1, yy1 : REALTYPE);
PROCEDURE RTLineWorldAbs( xx1, yy1: REALTYPE);
PROCEDURE RTLineWorldRel( xx1, yy1: REALTYPE );
(*
PROCEDURE RTBarWorld( xx1, yy1, h,w :REALTYPE; gc,gh : integer );
PROCEDURE RTHBar(xx1: REALTYPE; yy1:INTEGER; h: INTEGER;
                 w: REALTYPE; gc,gh : integer );
PROCEDURE RTVBar( xx1: INTEGER; yy1: REALTYPE; h: REALTYPE;
                 w :INTEGER; gc,gh : integer );
PROCEDURE RTBarTextWorld( xx1, yy1,h,w :REALTYPE;
                          s: string; bc,tc : integer );
*)
PROCEDURE RTpolyLineWorldAbs( x, y: TPOINTER_C; numdat : integer);
PROCEDURE RTpolyLineWorldRel( x,y : RTMarkerType; numdat : iNTEGER);
PROCEDURE RTpolyFillWorldAbs( x,y : TPOINTER_C;
                           fillstyle, fillcolor, numdat : INTEGER);

FUNCTION  RTPowerCalc( realnum: REALTYPE;
                    power :  REALTYPE ):  REALTYPE;
FUNCTION  RTlog( realnum:  REALTYPE) :  REALTYPE;
FUNCTION  RTInRegion(x,y, x1,y1,x2,y2: INTEGER): BOOLEAN;

FUNCTION  RTFindWindow(win: RTStatPntr; x,y: INTEGER): BOOLEAN;
PROCEDURE RTSetGraphAreaWorld(win: RTStatPntr; a,b,c,d : REALTYPE);
FUNCTION  RTWinPWidth(win: RTStatPntr): INTEGER;
FUNCTION  RTWinWidth(win: RTStatPntr): REALTYPE;
FUNCTION  RTWinPHeight(win: RTStatPntr): INTEGER;
FUNCTION  RTWinWHeight(win: RTStatPntr): REALTYPE;
FUNCTION  RTPlotAreaPWidth(win: RTStatPntr): INTEGER;
FUNCTION  RTPlotAreaWWidth(win: RTStatPntr): REALTYPE;
FUNCTION  RTPlotAreaPHeight(win: RTStatPntr): INTEGER;
FUNCTION  RTPlotAreaWHeight(win: RTStatPntr): REALTYPE;
PROCEDURE RTSetWin2PlotRatio(win: RTStatPntr; l,t,r,b: REALTYPE);
PROCEDURE RTSetRect( VAR r: rect; xx1,yy1,xx2,yy2 : INTEGER );
PROCEDURE RTSetWinTextStyle(win: RTStatPntr; font, size: INTEGER );
PROCEDURE RTSDRVp(win: RTStatPntr);
PROCEDURE RTSPRVp(win: RTStatPntr);
PROCEDURE RTSPCW(win: RTStatPntr);
PROCEDURE RTSPWW(win: RTStatPntr);

FUNCTION  RTCheckRange(minval,maxval, inval: REALTYPE): BOOLEAN;
PROCEDURE RTTitleWin(win: RTStatPntr; title: string);
(*
PROCEDURE RTUnitsWindow(win: RTStatPntr; units: string;
                            x,y,orient: INTEGER);
PROCEDURE RTPIDTagWindow(win: RTStatPntr; tags: tagarraytype; y1,n: INTEGER);
PROCEDURE RTHTagWindow(win: RTStatPntr; tags: tagarraytype; y1,n: INTEGER);
PROCEDURE RTVTagWindow(win: RTStatPntr; tags: tagarraytype; n,side: INTEGER);
FUNCTION  RTCalcTagWidth(tags: tagArrayType; n: INTEGER): INTEGER;
*)
FUNCTION  RTCalcLabelWidth(minVal, maxVal: REALTYPE; decimals: INTEGER): INTEGER;
(*
PROCEDURE RTLineRatchetNI(x1,y1,x2,y2, mode: INTEGER);
PROCEDURE RTDrawRatchetPoly(n:word; data: RTBufferArrayType; mode: INTEGER);
PROCEDURE RTDrawAlarmLine(min, max, alarmval: REALTYPE;
                               hvmode,color: INTEGER);
*)
PROCEDURE RTInitWindowColors(win: RTStatPntr; pbgcol, vbgCol, lacol, hacol,
                             okcol,labcol,axiscol: INTEGER);
PROCEDURE SetRTComVars(win: RTStatPntr; RTType: RTGraphType;
	       lcolor, lfill: RTIntArrayType;
	       titstr: titleType; tags: tagArrayType;
	       Eunits: tagType;  numT: INTEGER;
               xdecs, ydecs: INTEGER);
FUNCTION  RTCalcYLabTics(min,max:INTEGER): INTEGER;
FUNCTION  RTCalcDecs(a1,a2 :  REALTYPE):INTEGER;
PROCEDURE RTLabelTicYString(win: RTStatPntr;  xx, yy  :  REALTYPE;
                           TicLabel: string;
                           dir     :  INTEGER );
PROCEDURE RTLabelTicY(win: RTStatPntr; x, y, yval:  REALTYPE;
                     decs,dir:  INTEGER );
PROCEDURE RTSetWindow(win : RTStatPntr );
PROCEDURE RTDefGraphWindow(win: RTStatPntr; xx1,yy1,xx2,yy2 :  INTEGER);
PROCEDURE RTBorderWindow(win: RTStatPntr; c: integer);
PROCEDURE RTSetViewBackground(win: RTStatPntr;c: integer);
PROCEDURE RTSetPlotBackground(win: RTStatPntr;c:integer);
PROCEDURE RTSetAxisDirection(win: RTStatPntr; axdir: INTEGER);
PROCEDURE RTScalePlotArea(win: RTStatPntr;xx1,yy1,xx2,yy2 :  REALTYPE );
PROCEDURE RTClearWindow(win: RTStatPntr);
PROCEDURE RTClearGraph(win: RTStatPntr);
PROCEDURE RTSetXYIntercepts(win: RTStatPntr; xx1,yy1:  REALTYPE);
PROCEDURE RTDrawTicX(win: RTStatPntr;  x,y,l :  REALTYPE );
PROCEDURE RTDrawTicY(win: RTStatPntr;  x,y,l:  REALTYPE);
FUNCTION  RTCalcTicXPix(majmin: INTEGER): INTEGER;
FUNCTION  RTCalcTicYPix(majmin: INTEGER): INTEGER;
FUNCTION  RTCalcTicX(win: RTStatPntr; majmin: INTEGER): REALTYPE;
FUNCTION  RTCalcTicY(win: RTStatPntr; majmin: INTEGER): REALTYPE;
PROCEDURE RTDrawXAxis(win: RTStatPntr; TicSpace:  REALTYPE; dir : INTEGER);
PROCEDURE RTDrawYAxis(win: RTStatPntr; TicSpace:  REALTYPE; dir : INTEGER);
PROCEDURE RTLabelXAxisMod(win: RTStatPntr; start: RealType;
                                NthTic, decs, dir :  INTEGER);
PROCEDURE RTLabelXAxis(win: RTStatPntr;  NthTic, decs, dir:  INTEGER);
PROCEDURE RTLabelYAxis(win: RTStatPntr;  NthTic,  decs, dir:  INTEGER);
PROCEDURE RTLableTicXString(win: RTStatPntr; x, y :  REALTYPE;
                           TicLabel : string;
                           dir  :  INTEGER );
PROCEDURE RTLabelTicX(win: RTStatPntr; x, y, xval : REALTYPE;
                      decs, dir: INTEGER);
PROCEDURE RTLabelXAxisRT(win: RTStatPntr; NthTic,  decs, dir:  INTEGER;
                         startv: RealType);
PROCEDURE RTLabelYAxisRT(win: RTStatPntr; NthTic,  decs, dir:  INTEGER;
                         startv: RealType);
PROCEDURE RTDrawGridX(win: RTStatPntr;  NthTic :  INTEGER);
PROCEDURE RTDrawGridY(win: RTStatPntr;  NthTic :  INTEGER);
PROCEDURE RTDrawGrid(win: RTStatPntr; NthTic :  INTEGER);

PROCEDURE RTSetPercentWindow(win: RTStatPntr; x1,y1,x2,y2 : REALTYPE);
PROCEDURE rtgetgraphmode(VAR gdriver, gmode: INTEGER);
FUNCTION  rtcheckprnon: BOOLEAN;
PROCEDURE rtforcegraphicsdriver(gdriver, gmode: INTEGER;
			   userbgi: string);
FUNCTION  RTGetBGIDir: string;
PROCEDURE RTOneTimeInit;

{ Ancien RTInitGraphics }
Constructor Init( numwin, dowinint, gmaxx, gmaxy: INTEGER);

PROCEDURE RTFreeWindow(VAR w: rtstatpntr);
PROCEDURE rtsetrealbufferflag(win: rtstatpntr;  f: INTEGER);
PROCEDURE RTInitWindow( VAR win: RTStatPntr);

{Ancien RTCloseGraphics }
Destructor Done; virtual;

PROCEDURE RTSetFGBGColors(fg,bg: INTEGER);
PROCEDURE RTInitGraphTextStuff;
PROCEDURE RTWriteIText(x,y: INTEGER;
                       lastVal, newVal: REALTYPE; fc,bc,decimals: INTEGER);
PROCEDURE RTWriteStr(x,y: INTEGER;
                        oldStr, newStr: string; fc,bc: INTEGER);
(*
FUNCTION  RTCalcAlarmState(la,ha,cv: REALTYPE): INTEGER;
FUNCTION  RTDeltaBar(old, new, center: REALTYPE): INTEGER;
FUNCTION  RTCalcAlarmColor(win: RTStatPntr; alarmState: INTEGER): INTEGER;
*)
PROCEDURE RTPixToViewport(win: RTStatPntr; drpr,xp,yp:
                          INTEGER; VAR xvp,yvp: INTEGER);
PROCEDURE RTPixToWorld(win: RTStatPntr; xp,yp: INTEGER; VAR xw,yw: REALTYPE);
PROCEDURE RTGraphGotoXY(x,y: INTEGER);
FUNCTION  RTGraphWhereX: INTEGER;
FUNCTION  RTGraphWhereY: INTEGER;
PROCEDURE RTGraphBox(x,y,numCharX, numCharY,color: INTEGER);
PROCEDURE RTGraphTextBox(left, top, w, h, lineStyle: WORD);
PROCEDURE RTGraphClrEol;
PROCEDURE RTGraphWriteLJ(s: string; ww: INTEGER);
PROCEDURE RTGraphWriteRJ(s: string; ww: INTEGER);
PROCEDURE RTGraphWrite(s: string; ww,just: INTEGER);
PROCEDURE RTGraphWriteInt(i: INTEGER; ww: INTEGER);
PROCEDURE RTGraphWriteReal(r: REAL; ww,decs: INTEGER);
PROCEDURE RTSetGraphTextColors(fg,bg: INTEGER);
PROCEDURE RTReverseVideoOn;
PROCEDURE RTReverseVideoOff;
(*
FUNCTION  RTGetHTagChannel(win: RTStatPntr; xp,yp: INTEGER): INTEGER;
FUNCTION  RTGetVTagChannel(win: RTStatPntr; xp,yp,side: INTEGER): INTEGER;
*)
PROCEDURE RTClearWindowData(VAR w: rtstatpntr);

end; { TRT type definition }


IMPLEMENTATION

(* GRAPH simulation *)

(* QCPRN *)
PROCEDURE getprndrivername(drnum: INTEGER; VAR drstring: STRING);
begin end;
FUNCTION  scaleprnfont(grmode, font, basesize: INTEGER): INTEGER;
begin end;
PROCEDURE qcsetupprinter( prnport,orient,formfeed, quietmode: INTEGER;
		          filename: string;VAR   colormap, comportparams,
		          sizeoverrides: PrnSetupType; VAR error: INTEGER);
begin end;

PROCEDURE TRT.InitGraph;
begin

  Pen := CreatePen(ps_Solid, 1, BasicColors[0]);
  OldPen := SelectObject( WinDC, Pen );
  PenStyle := ps_Solid; PenThick := 1; PenColor := 0;

  BrushColor := 15; BrushFill := SolidFill;
  with Logbrush do begin
    lbStyle := bs_solid;
    lbColor := BasicColors[BrushColor];
    lbHatch := hs_vertical;
  end;
  Brush := CreateBrushIndirect(LogBrush);
  OldBrush := SelectObject(WinDC, Brush);
  SetBkMode(WinDC, TRANSPARENT);

  WinText.Font      := 1;
  WinText.Direction := 0; { Always 0=Horiz by default }
  Wintext.CharSize  := 1;
  WinFont := 0; WinFont := CreateFontIndirect(WinLogFont);
  OldFont := SelectObject( WinDC, WinFont );
end;

PROCEDURE TRT.CloseGraph;
begin
  SelectObject( WinDC, OldPen);
  DeleteObject(Pen);
  SelectObject( WinDC, OldBrush);
  DeleteObject(Brush);
  SelectObject( WinDC, OldFont);
  DeleteObject(WinFont);
end;

PROCEDURE TRT.DetectGraph(var GraphDriver, GraphMode: Integer);
begin GraphDriver := 0; GraphMode := 0; end;
function  TRT.GraphResult: Integer;
begin GraphResult := 0; end;
function  TRT.GraphErrorMsg(ErrorCode: Integer): string;
begin GraphErrorMsg := ''; end;
function  TRT.InstallUserDriver(Name: string; AutoDetectPtr: pointer): Integer;
begin InstallUserDriver := 0; end;
function  TRT.GetDriverName: string;
begin GetDriverName := ''; end;
procedure TRT.SetWriteMode(WriteMode:Integer);
begin end;

PROCEDURE TRT.GetViewSettings(var ViewPort: ViewPortType);
begin ViewPort := WinPort end;
PROCEDURE TRT.SetViewPort(x1, y1, x2, y2: Integer; Clip: Boolean);
begin
  WinPort.x1 := x1;
  WinPort.x2 := x2;
  WinPort.y1 := y1;
  WinPort.y2 := y2;
  WinPort.Clip := Clip;
end;
PROCEDURE TRT.ClearViewPort;
begin end;

PROCEDURE TRT.GetAspectRatio(var Xasp, Yasp: Word);
begin Xasp := 1; Yasp := 1; end;

PROCEDURE TRT.GetPalette(var Palette: PaletteType);
begin Palette.size := 16; end;

function  TRT.GetMaxColor: Word;
begin GetMaxColor := 15; end;

PROCEDURE TRT.SetFillStyle(Pattern: Word; Color: Word);
begin
  BrushColor := Color; BrushFill := Pattern;
  with Logbrush do begin
    lbStyle := bs_solid;
    lbColor := BasicColors[BrushColor];
    lbHatch := hs_vertical;
  end;
  SelectObject( WinDC, OldBrush);
  DeleteObject(Brush);
  if Pattern <= 1 then begin
    Brush := CreateBrushIndirect(LogBrush);
  end else begin
    Brush := CreateHatchBrush(FillStyles[Pattern], BasicColors[BrushColor]);
  end;
  OldBrush := SelectObject(WinDC, Brush);
end;

procedure TRT.GetFillSettings(var FillInfo:FillSettingsType);
begin
  with FillInfo do begin
    Pattern := BrushFill;
    Color   := BrushColor;
  end;
end;

PROCEDURE TRT.SetLineStyle(LineStyle: Word; Pattern: Word; Thickness: Word);
label 999;
begin

  if not(LineStyle in [0..3]) then goto 999;
  PenStyle := LineStyle; PenThick := Thickness;
  if Thickness <> Normwidth then PenStyle := ps_solid;

  SelectObject( WinDC, OldPen);
  DeleteObject(Pen);
  Pen := CreatePen(PenStyle, PenThick, BasicColors[PenColor]);
  OldPen := SelectObject( WinDC, Pen );
999:end;

PROCEDURE TRT.SetColor(Color: Word);
begin
  PenColor := Color; TxtFC := Color;
  SelectObject( WinDC, OldPen);
  DeleteObject(Pen);
  Pen := CreatePen(PenStyle, PenThick, BasicColors[PenColor]);
  OldPen := SelectObject( WinDC, Pen );
end;

FUNCTION TRT.GetColor: Word;
begin GetColor := PenColor end;

function TRT.GetX: Integer;
begin end;
function TRT.GetY: Integer;
begin end;

function  TRT._GetPixel(X, Y: Integer):Word;
var
  i : integer;
  RGB_Col: TColorRef;
begin
  RGB_Col := GetPixel(WinDC, WinPort.x1+X, WinPort.y1+Y);
  _GetPixel := 0;
  for i := 0 to 15 do
    if RGB_Col = BasicColors[i] then _GetPixel := i;
end;

procedure TRT.PutPixel(X, Y: Integer; Pixel:Word);
begin
  SetPixel(WinDC,WinPort.x1+X,WinPort.y1+Y,BasicColors[Pixel]);
end;

PROCEDURE TRT._MoveTo(X, Y: Integer);
begin  MoveTo(WinDC,WinPort.x1+X,WinPort.y1+Y);
       WhereX := WinPort.x1+X;
       WhereY := WinPort.y1+Y;
 (* writeln('MOVETO : ',X,' ',Y); *)
end;
PROCEDURE TRT._MoveRel(Dx, Dy: Integer);
var X1,Y1:integer;
begin  X1 := WhereX + Dx;
       Y1 := WhereY + Dy;
       MoveTo(WinDC,X1,Y1);
       WhereX := X1;
       WhereY := Y1;
end;
PROCEDURE TRT._LineTo(X, Y: Integer);
begin  LineTo(WinDC,WinPort.x1+X,WinPort.y1+Y);
       WhereX := WinPort.x1+X;
       WhereY := WinPort.y1+Y;
 (*  writeln('LINETO : ',X,' ',Y); *)
end;
PROCEDURE TRT._LineRel(Dx, Dy: Integer);
var X1,Y1:integer;
begin  X1 := WhereX + Dx;
       Y1 := WhereY + Dy;
       LineTo(WinDC,X1,Y1);
       WhereX := X1;
       WhereY := Y1;
end;
PROCEDURE TRT.Line(x1, y1, x2, y2: Integer);
begin
       _MoveTo(x1, y1);
       _LineTo(x2, y2);
       WhereX := x2;
       WhereY := y2;
end;
PROCEDURE TRT._rectangle(x1, y1, x2, y2: Integer);
var DrawMode : Integer;
    Pen_Style, Pen_Thick, Pen_Color : Word;
begin

  Pen_Style := PenStyle; Pen_Thick := PenThick; Pen_Color := PenColor;
  SelectObject( WinDC, OldPen);
  DeleteObject(Pen);
  Pen := CreatePen(ps_Solid, 1, LogBrush.lbColor);
  OldPen := SelectObject( WinDC, Pen );
(*
  DrawMode := GetROP2(WinDC);
  SetROP2(WinDC, R2_COPYPEN);
*)
  Rectangle(WinDC,WinPort.x1+x1,WinPort.y1+y1,
                      WinPort.x1+x2+1,WinPort.y1+y2+1);
(*
  SetROP2(WinDC, DrawMode);
*)
  PenStyle := Pen_Style; PenThick := Pen_Thick; PenColor := Pen_Color;
  SelectObject( WinDC, OldPen);
  DeleteObject(Pen);
  Pen := CreatePen(PenStyle, PenThick, BasicColors[PenColor]);
  OldPen := SelectObject( WinDC, Pen );

end;

PROCEDURE TRT.Bar(x1, y1, x2, y2: Integer);
var DrawMode : Integer;
    Pen_Style, Pen_Thick, Pen_Color : Word;
begin
  Pen_Style := PenStyle; Pen_Thick := PenThick; Pen_Color := PenColor;
  SelectObject( WinDC, OldPen);
  DeleteObject(Pen);
  Pen := CreatePen(ps_Solid, 1, LogBrush.lbColor);
  OldPen := SelectObject( WinDC, Pen );
(*
  DrawMode := GetROP2(WinDC);
  SetROP2(WinDC, R2_COPYPEN);
*)
  if BrushFill > 1 then begin
    SetBkMode(WinDC, OPAQUE);
    SetBkColor(WinDC,BasicColors[0]);
  end;
  Rectangle(WinDC,WinPort.x1+x1,WinPort.y1+y1,
                      WinPort.x1+x2+1,WinPort.y1+y2+1);
  if BrushFill > 1 then SetBkMode(WinDC, TRANSPARENT);
(*
  SetROP2(WinDC, DrawMode);
*)
  PenStyle := Pen_Style; PenThick := Pen_Thick; PenColor := Pen_Color;
  SelectObject( WinDC, OldPen);
  DeleteObject(Pen);
  Pen := CreatePen(PenStyle, PenThick, BasicColors[PenColor]);
  OldPen := SelectObject( WinDC, Pen );
end;

PROCEDURE TRT.DrawPoly(NumPoints: Word; var PolyPoints);
TYPE point2D = RECORD x,y : INTEGER; END;   (* in q_Worldd *)
     Poly2DType = ARRAY[0..100] OF point2D;
VAR polyVector : Poly2DType absolute Polypoints;
    i : integer;
begin
  for i := 0 to NumPoints-1 do begin
    polyVector[i].x := WinPort.x1+polyVector[i].x;
    polyVector[i].y := WinPort.y1+polyVector[i].y;
  END;
  Polyline(WinDC, PolyPoints, NumPoints);
  for i := 0 to NumPoints-1 do begin
    polyVector[i].x := polyVector[i].x-WinPort.x1;
    polyVector[i].y := polyVector[i].y-WinPort.y1;
  END;
end;

PROCEDURE TRT.FillPoly(NumPoints: Word; var PolyPoints);
TYPE point2D = RECORD x,y : INTEGER; END;   (* in q_Worldd *)
     Poly2DType = ARRAY[0..100] OF point2D;
VAR polyVector : Poly2DType absolute Polypoints;
    i : integer;
    Pen_Style, Pen_Thick, Pen_Color : Word;
begin
  for i := 0 to NumPoints-1 do begin
    polyVector[i].x := WinPort.x1+polyVector[i].x;
    polyVector[i].y := WinPort.y1+polyVector[i].y;
  END;
  if BrushFill > 1 then begin
    SetBkMode(WinDC, OPAQUE);
    SetBkColor(WinDC,BasicColors[0]);
  end;
  Polygon(WinDC, PolyPoints, NumPoints);
  if BrushFill > 1 then SetBkMode(WinDC, TRANSPARENT);
  for i := 0 to NumPoints-1 do begin
    polyVector[i].x := polyVector[i].x-WinPort.x1;
    polyVector[i].y := polyVector[i].y-WinPort.y1;
  END;
end;

procedure TRT.PieSlice(X, Y: Integer; StAngle,EndAngle, Radius: Word);
begin
  if BrushFill > 1 then begin
    SetBkMode(WinDC, OPAQUE);
    SetBkColor(WinDC,BasicColors[0]);
  end;
  Pie(WinDC, WinPort.x1+X-Radius, WinPort.y1+Y-Radius,
             WinPort.x1+X+Radius, WinPort.y1+Y+Radius,
             WinPort.x1+X+ round(Radius*cos(TWO_PI*StAngle)),
             WinPort.y1+Y- round(Radius*sin(TWO_PI*StAngle)),
             WinPort.x1+X+ round(Radius*cos(TWO_PI*EndAngle)),
             WinPort.y1+Y- round(Radius*sin(TWO_PI*EndAngle)));
  if BrushFill > 1 then SetBkMode(WinDC, TRANSPARENT);
end;

PROCEDURE TRT.SetTextStyle(Font, Direction: Word; CharSize: Word);
begin   
    WinText.Font      := Font;
    WinText.Direction := Direction; { Always 0=Horiz by default }
    Wintext.CharSize  := Charsize;
  with WinLogFont do begin
    lfHeight        := 12*Charsize;
    lfWidth         := 8*Charsize;
    lfEscapement    := 0;
    if Direction = 1 then lfEscapement := 900;
  end;
  SelectObject( WinDC, OldFont);
  DeleteObject(WinFont);
  WinFont := CreateFontIndirect(WinLogFont);
  OldFont := SelectObject(WinDC, WinFont);
end;
PROCEDURE TRT.SetTextJustify(Horiz, Vert: Word);
begin
    WinText.Horiz      := Horiz;
    WinText.Vert       := Vert;
end;
PROCEDURE TRT.GetTextSettings(var TextInfo: TextSettingsType);
begin TextInfo := Wintext; end;
function TRT.TextHeight(TextString: string): Word;
var
    TM: TTextMetric;
begin
    if not(MetaFile) then begin
      GetTextMetrics(WinDC, TM);
      TextHeight := TM.tmHeight; TxtH := TM.tmHeight;
    end else TextHeight := TxtH;
end;
function TRT.TextWidth(TextString: string): Word;
var
    TM: TTextMetric;
begin
    if not(MetaFile) then begin
      GetTextMetrics(WinDC, TM);
      TextWidth := TM.tmMaxCharWidth*length(TextString);
      TxtW := TM.tmMaxCharWidth;
    end else TextWidth := TxtW*length(TextString);
end;

PROCEDURE TRT.OutText(TextString: string);
var
    Texte : array[0..79] of Char;
    TextChar : array[0..0] of Char;
    i,StrLong : Integer;
    AlignX,AlignY : Integer;
    TM: TTextMetric;
    w_height,w_width:integer;
    WX_, WY_ : Integer;
begin
    if not(MetaFile) then begin
      GetTextMetrics(WinDC, TM);
      w_Width := TM.tmMaxCharWidth;
      w_Height := TM.tmHeight;
    end else begin
      w_Width := TxtW;
      w_Height := TxtH;
    end;

{ Valuse for BGI Font param.
      LeftText   =   0;
      BottomText =   0;
      CenterText =   1;
      RightText  =   2;
      TopText    =   2;
}
    AlignX := TA_LEFT;
    if WinText.Horiz = 1 then AlignX := TA_CENTER;
    if WinText.Horiz = 2 then AlignX := TA_RIGHT;
    AlignY := TA_BOTTOM;
    if WinText.Vert  = 1 then AlignY := TA_BASELINE;
    if WinText.Vert  = 2 then AlignY := TA_TOP;
    SetTextAlign(WinDC, AlignX OR AlignY);

    if TxtFC < 0 then TxtFC := 0; { Default Pen colour }
    if TxtFC < 16 then SetTextColor(WinDC, BasicColors[TxtFC]);
    SetBkColor(WinDC, BasicColors[15]);

    StrLong := length(TextString);
    If StrLong > 80 then StrLong := 80;
    StrPcopy(Texte, TextString);
    WY_ := WhereY + round(w_Height*0.33);
    if WinLogFont.lfEscapement = 0 then begin
        WX_ := WhereX;
      TextOut(WinDC, WX_, WY_, Texte, StrLong);
    end else begin
        WX_ := WhereX + w_Height div 2;
        TextOut(WinDC, WX_, WY_, Texte,StrLong);
    end;
end;

function TRT.ImageSize(x1, y1, x2, y2:Integer): Word;
begin
  ImageSize := 0;
end;
procedure TRT.GetImage(x1, y1, x2, y2:Integer; var BitMap);
begin end;
procedure TRT.PutImage(X, Y: Integer; var BitMap; BitBlt: Word);
begin end;


(* ---------------- *)

FUNCTION TRT.RTGetNumWin: INTEGER;
BEGIN
  RTGetNumWin := rtnumwin;
END;
FUNCTION TRT.RTTH(n: Realtype): INTEGER;
BEGIN
  RTTH := round(textheight('H')*n);
END;

PROCEDURE TRT.RTSetIntrMaskFlag(f: INTEGER);
BEGIN
  intrmskf := f;
END;

PROCEDURE TRT.RTIntrOff;
BEGIN
  if (intrmskf = 1) THEN
  port[$21] := IntrOffMask;
END;

PROCEDURE TRT.RTIntrOn;
BEGIN
  if (intrmskf = 1) THEN
  port[$21] := IntrOnMask;
END;

FUNCTION TRT.RTInGraphMode: BOOLEAN;
BEGIN
  IF Length(GetDrivername)> 2 THEN RTInGraphMode := TRUE ELSE
  RTInGraphMode := FALSE;
END;

PROCEDURE TRT.RTSetFGBGColors(fg,bg: INTEGER);
BEGIN
  txtBC := bg;  txtFC := fg;
END;

PROCEDURE TRT.RTInitGraphTextStuff;
BEGIN
  SetTextStyle(0,0,1);
  SetTextJustify(0,0);
  TextWCell := Textwidth('X');
  TextHCell := Textheight('X');
  RTSetFGBGColors(getmaxcolor,0);
END;

PROCEDURE TRT.RTGraphGotoXY(x,y: INTEGER);
BEGIN
  x := (x-1) * TextWCell+4;
  y := (y-1) * TextHCell+4;
  _MoveTo(x,y);
END;

FUNCTION TRT.RTGraphWhereX: INTEGER;
BEGIN
  RTGraphWhereX := (GetX DIV TextWCell)+1;
END;

FUNCTION TRT.RTGraphWhereY: INTEGER;
BEGIN
  RTGraphWhereY := (GetY DIV TextHCell)+1;
END;

PROCEDURE TRT.RTGraphBox(x,y,numCharX, numCharY,color: INTEGER);
VAR l,h: INTEGER;
BEGIN
  l := TextWCell * numCharX;
  h := TextHCell;
  x := TextWCell * (x-1)+4;
  y := TextHCell * (y-1)+4;
  SetFillStyle(1,txtbc);
  BAR(x,y,x+l-1,y+h-1);
END;

PROCEDURE TRT.RTGraphTextBox(left, top, w, h, lineStyle: WORD);
VAR i,j: WORD;
BEGIN
   w := w * TextWCell;
   left := (left-1) * TextWCell;
   h := h * TextHCell;
   top := (top-1) * TextHCell;
   BAR(left, top, left + w-1, top + h-1) ;
   _rectangle(left, top, left + w-1, top + h-1);
END;

PROCEDURE TRT.RTGraphClrEol;
VAR x,y: INTEGER;
BEGIN
   x := RTGraphWhereX; y := RTGraphWhereY;
   RTGraphBox(x,y,80-x,1,0);
   RTGraphGotoXY(x,y);
END;

PROCEDURE TRT.RTGraphWriteLJ(s: string; ww: INTEGER);
VAR x,y,h,i,l: INTEGER;
BEGIN
  SetTextJustify(LeftText,TopText);
  x := RTGraphWhereX; y := RTGraphWhereY;
  l := Length(s);
  IF ww = 0 THEN ww := l;
  RTGraphBox(x,y,ww,1,0);
  FOR i := l+1 TO ww DO
    s := s + #32;
  RTGraphGotoxy(x,y);
  RTSetColor(txtfc);
  RTOutTextNI(s);
  SetTextJustify(LeftText,BottomText);
END;

PROCEDURE TRT.RTGraphWriteRJ(s: string; ww: INTEGER);
VAR x,y,h,i,l: INTEGER;
BEGIN
  SetTextJustify(RightText,TopText);
  x := RTGraphWhereX; y := RTGraphWhereY;
  l := Length(s);
  IF ww = 0 THEN ww := l;
  RTGraphBox(x,y,-ww,1,0);
  FOR i := l+1 TO ww DO
    s :=  #32+s;
  RTGraphGotoxy(x,y);
  RTSetColor(txtfc);
  RTOutTextNI(s);
  SetTextJustify(LeftText,BottomText);
END;

PROCEDURE TRT.RTGraphWrite(s: string; ww,just: INTEGER);
BEGIN
  RTSetColor(txtfc);
  IF just = 0 THEN
    RTGraphWriteLJ(s,ww)
  ELSE IF just = 2 THEN
    RTGraphWriteRJ(s,ww);
END;

PROCEDURE TRT. RTGraphWriteInt(i: INTEGER; ww: INTEGER);
BEGIN
  STR(i:ww,strw);
  RTGraphWrite(strw,0,0);
END;

PROCEDURE TRT.RTGraphWriteReal(r: REAL; ww,decs: INTEGER);
BEGIN
  STR(r:ww:decs,s);
  RTGraphWrite(s,0,0);
END;

PROCEDURE TRT.RTSetGraphTextColors(fg,bg: INTEGER);
BEGIN
  RTIntrOff;
  RTSetColor(fg);
  SetFillStyle(1,bg);
  RTSetFGBGColors(fg,bg);
  RTIntrOn;
END;

PROCEDURE TRT.RTReverseVideoOn;
BEGIN
  RTsetGraphTextColors(txtBC,txtFC);
END ;

PROCEDURE TRT.RTReverseVideoOff;
BEGIN
  RTsetGraphTextColors(txtFC, txtBC);
END ;

PROCEDURE TRT.RTGraphReadln(VAR s: string);
BEGIN
END ;

PROCEDURE TRT.RTGraphReadReal(VAR r: REALTYPE);
BEGIN
END;

PROCEDURE TRT.RTGraphReadInt(VAR i: INTEGER);
BEGIN
END;

FUNCTION TRT.RTViewportWidth: INTEGER;
BEGIN
  RTViewportWidth := vpl;
END;

FUNCTION TRT.RTViewportHeight: INTEGER;
BEGIN
  RTViewportHeight := vph;
END;

PROCEDURE TRT.RTClipOn;
BEGIN
  clipFlag := TRUE;
END;

PROCEDURE TRT. RTClipOff;
BEGIN
  clipFlag := FALSE;
END;


PROCEDURE TRT.rtgetgraphmode(VAR gdriver, gmode: INTEGER);
BEGIN
END;

FUNCTION TRT.rtcheckprnon: BOOLEAN;
BEGIN
END;

{ force graphics driver to a certain driver and mode }
PROCEDURE TRT.rtforcegraphicsdriver(gdriver, gmode: INTEGER;
			   userbgi: string);
BEGIN
END;

FUNCTION TRT.RTGetBGIDir: STRING;
BEGIN
END;

PROCEDURE TRT.RTOneTimeInit;
VAR x,y, Gdriver, Gmode, Gerror:  INTEGER;
    p: PaletteType;
BEGIN
  RTSetGraphViewport(0,0,GetMaxX, GetMaxY);
  RTInitGraphTextStuff;
  SetTextJustify( LeftText , CenterText);
  IF (gdr <= -16) THEN settextstyle(SMALLFONT,0,4)
  ELSE settextstyle(0,0,1);
  GetPalette(p);
  maxColor := p.size-1;
END;

PROCEDURE TRT.RTSetMouseIntr(intnum: INTEGER);
BEGIN
END;

PROCEDURE TRT.checkoor(VAR ylow, yhigh: REALTYPE; minrange: REALTYPE);
BEGIN
  IF (abs(yhigh-ylow) < minrange) THEN
  BEGIN
    ylow := 0.0;
    yhigh := 1.0;
  END;
END;

PROCEDURE TRT. RTBarNI(x1,y1,x2,y2: INTEGER);
BEGIN
  RTIntrOff;
  Bar(x1,y1,x2,y2);
  RTIntrOn;
END;


PROCEDURE TRT. RTRectangleNI(x1,y1,x2,y2: INTEGER);
BEGIN
  RTIntrOff;
  _rectangle(x1,y1,x2,y2);
  RTIntrOn;
END;

PROCEDURE TRT. RTLineToNI(x1,y1: INTEGER);
BEGIN
  RTIntrOff;
  _LineTo(x1,y1);
  RTIntrOn;
END;

PROCEDURE TRT. RTLineNI(x1,y1,x2,y2: INTEGER);
BEGIN
  RTIntrOff;
  Line(x1,y1,x2,y2);
  RTIntrOn;
END;

PROCEDURE TRT. RTLineRelNI(x1,y1: INTEGER);
BEGIN
  RTIntrOff;
  _LineRel(x1,y1);
  RTIntrOn;
END;

PROCEDURE TRT. RTOutTextNI(s:string);
BEGIN
  RTIntrOff;
  OutText(s);
  RTIntrOn;
END;

PROCEDURE TRT. RTSetColor(c: INTEGER);
BEGIN
  RTIntrOff;
  IF c > maxcolor THEN c := maxColor;
  SetColor(c);
  RTIntrOn;
END;

PROCEDURE TRT. RTDrawPolyNI(n: word; VAR pnts);
BEGIN
  RTIntrOff;
  DrawPoly(n,pnts);
  RTIntrOn;
END;

PROCEDURE TRT. RTBarXX(x1, y1, x2, y2: INTEGER);
BEGIN
   y1 := vph - y1;
   y2 := vph - y2;
   RTBarNI(x1,y1, x2, y2);
   RTRectangleNI(x1,y1, x2, y2);
END;

PROCEDURE TRT. RTBarXY( xx1, yy1,w,h, oc,gc,gh : integer );
BEGIN
  SetFillStyle( gh,gc );
  RTSetColor(gc);
  RTBarNI(xx1, vph-yy1,xx1+w,vph-yy1-h);
  RTSetColor(oc);
  RTRectangleNI(xx1, vph-yy1,xx1+w,vph-yy1-h);
END;

PROCEDURE TRT. RTRectangleXX(x1, y1, x2, y2: REALTYPE);
BEGIN
  RTRectangleNI(trunc(x1),vph-trunc(y1), trunc(x2), vph-trunc(y2));
END;

PROCEDURE TRT. RTGetMaxCoords(VAR x,y: integer);
BEGIN
  x := GetMaxX; Y := GetmaxY;
END;

PROCEDURE TRT. RTLineToXX(x,y: INTEGER);
BEGIN
  RTLineToNI(x,vph-y);
END;

PROCEDURE TRT. RTLineRelXX(x,y: INTEGER);
BEGIN
  _LineRel(x,-y);
END;

PROCEDURE TRT. RTMoveToXX(x,y: INTEGER);
BEGIN
  _MoveTo(x,vph-y);
END;

PROCEDURE TRT. RTMoveRelXX(x,y: INTEGER);
BEGIN
  _MoveRel(x,-y);
END;

PROCEDURE TRT.RTsetWorldRect( VAR r: worldRect;
                       a,b,c,d : REALTYPE );
BEGIN
  r.left := a;
  r.bottom := b;
  r.right := c;
  r.top := d;
END;

PROCEDURE TRT.RTGetViewport( VAR xx1, yy1, xx2, yy2 : INTEGER );
VAR gp: viewporttype;
BEGIN
  GetViewSettings(gp);
  xx1 := gp.x1;
  yy1 := gp.y1;
  xx2 := gp.x2;
  yy2 := gp.y2;
END;

PROCEDURE TRT.RTSetGraphViewport(xx1,yy1,xx2,yy2: integer);
VAR maxX, maxY, pux1, pux2, puy1, puy2 : integer;
     x1,x2,y1,y2 : REALTYPE;
BEGIN
  SetViewPort( xx1,yy1,xx2,yy2, clipFlag );
  vph := yy2 - yy1;
  vpl := xx2 - xx1;
END;

PROCEDURE TRT. RTSetWorldCoordinates(x1,y1,x2,y2 : RealType);
VAR wvp: ViewportType;
BEGIN
  wx := x1;
  wy := y1;
  wh := y2 - y1;
  wl := x2 - x1;
  tx := vpl/wl;
  ty := vph/wh;
END;

FUNCTION TRT.RTClampRealToInt( r: REALTYPE; l,h : INTEGER): INTEGER;
VAR _result: REALTYPE;
BEGIN
  IF (r<l ) THEN _result := l
  ELSE
    IF (r>h) THEN _result := h
    ELSE _result := r;
  RTClampRealToInt := trunc(_result+0.0000001);
END;

FUNCTION TRT.RTClampReal( r: REALTYPE; l,h : RealType): RealType;
VAR _result: REALTYPE;
BEGIN
  IF (r<l ) THEN _result := l
  ELSE
    IF (r>h) THEN _result := h
    ELSE _result := r;
  RTClampReal := _result;
END;

FUNCTION TRT.RTConvertX1( xx1 : REALTYPE ):  INTEGER;
var temp : REALTYPE;
BEGIN
  temp := (xx1-wx)*tx;
  RTConvertX1 := RTClampRealToInt(temp, -10000,10000);
END;


FUNCTION TRT.RTConvertY1(yy1: REALTYPE ) :INTEGER;
VAR temp : REALTYPE;
BEGIN
  temp := (yy1-wy)*ty;
  RTConvertY1 :=  RTClampRealToInt(temp, -10000,10000) ;
END;


FUNCTION TRT.RTConvertX2( xx1: REALTYPE ): INTEGER;
var temp : REALTYPE;
BEGIN
  temp := xx1*tx;
  RTConvertX2 := RTClampRealToInt(temp, -10000, 10000 );
END;


FUNCTION TRT.RTConvertY2( yy1:  REALTYPE ) :INTEGER;
var temp :REALTYPE;
BEGIN
  temp := yy1*ty;
  RTConvertY2 := RTClampRealToInt(temp, -10000,10000);
END;

PROCEDURE TRT.RTMoveWorldAbs( xx1, yy1 : REALTYPE);

BEGIN
  RTMoveToXX(RTConvertX1(xx1),  RTConvertY1(yy1));
END;

PROCEDURE TRT. RTMoveWorldRel(xx1, yy1 : REALTYPE);
BEGIN
  RTMoveRelXX(RTConvertX2(xx1), RTConvertY2(yy1));
END;

PROCEDURE TRT. RTLineWorldAbs( xx1, yy1: REALTYPE);
BEGIN
  RTLineToXX(RTConvertX1(xx1), RTConvertY1(yy1));
END;

PROCEDURE TRT. RTLineWorldRel( xx1, yy1: REALTYPE );
BEGIN
  RTLineRelXX(RTConvertX2(xx1), RTConvertY2(yy1));
END;
(*
PROCEDURE TRT. RTBarWorld( xx1, yy1, h,w :REALTYPE; gc,gh : integer );
VAR rx1, rx2, ry1, ry2: INTEGER;
BEGIN
  rx1 := RTConvertX1(xx1);
  ry1 := RTConvertY1(yy1);
  rx2 := RTConvertX1(xx1 + w);
  ry2 := RTConvertY1(yy1 + h);
  SetFillStyle( gh,gc );
  RTSetColor(gc);
  RTBarXX(rx1, ry1,rx2,ry2);
END;

PROCEDURE TRT. RTHBar(xx1: REALTYPE; yy1:INTEGER; h: INTEGER;
                 w: REALTYPE; gc,gh : integer );
VAR rx1, rx2, ry2: INTEGER;
BEGIN
  rx1 := RTConvertX1(xx1);
  rx2 := RTConvertX1(xx1 + w);
  ry2 := yy1 + h;
  SetFillStyle( gh,gc );
  RTSetColor(gc);
  RTBarXX(rx1, yy1,rx2,ry2);
END;

PROCEDURE TRT. RTVBar( xx1: INTEGER; yy1: REALTYPE; h: REALTYPE;
                 w :INTEGER; gc,gh : integer );
VAR rx2, ry1, ry2: INTEGER;
BEGIN
  ry1 := RTConvertY1(yy1);
  rx2 := xx1 + w;
  ry2 := RTConvertY1(yy1 + h);
  SetFillStyle( gh,gc );
  RTSetColor(gc);
  RTBarXX(xx1, ry1,rx2,ry2);
END;

PROCEDURE TRT. RTBarTextWorld( xx1, yy1,h,w :REALTYPE;
                          s: string; bc,tc : integer );
VAR rx1, rx2, ry1, ry2: INTEGER;
BEGIN
  rx1 := RTConvertX1(xx1);
  ry1 := RTConvertY1(yy1);
  rx2 := RTConvertX1(xx1 + w);
  ry2 := RTConvertY1(yy1 + h);
  SetFillStyle( 1,bc );
  RTSetColor(bc);
  RTBarXX(rx1, ry1,rx2,ry2);
  RTMoveToXX(rx1+(rx2-rx1) div 2, ry1 + (ry2-ry1) div 2);
  SetTextJustify(1,1);
  RTSetColor(tc);
  OutText(s);
  SetTextJustify(0,0);
END;
*)
PROCEDURE TRT. RTpolyLineWorldAbs( x, y: TPOINTER_C;
                            numdat : integer);
VAR
  i : integer;
  so: word;
  xi,yi: REALTYPE;
BEGIN
  xi :=  x.GetPntrReal(0);
  yi :=  y.GetPntrReal(0);
  RTmoveworldabs(xi, yi);
  FOR i := 0 TO numdat - 1 DO
  BEGIN
    xi :=  x.GetPntrReal(i);
    yi :=  y.GetPntrReal(i);
    RTlineworldabs(xi, yi);
  END;
END;

PROCEDURE TRT. RTpolyLineWorldRel( x,y : RTMarkerType;
                           numdat : iNTEGER);
VAR
  i : integer;
BEGIN
  RTmoveworldrel(x[0], y[0]);
  FOR i := 1 TO numdat - 1 DO
    RTlineworldRel(x[i], y[i]);
END;

PROCEDURE TRT. RTpolyFillWorldAbs( x,y : TPOINTER_C;
                           fillstyle, fillcolor, numdat : INTEGER);
VAR polyVector : Poly2DType;
    i : INTEGER;
BEGIN
  RTmoveworldabs(x.GetPntrReal(0), y.GetPntrReal(0));
  FOR i := 0 TO numdat - 1 DO
  BEGIN
    polyVector[i].x := trunc(RTConvertX1(x.GetPntrReal(i)));
    polyVector[i].y := vph-trunc(RTConvertY1(y.GetPntrReal(i)));
  END;
  SetFillStyle(fillstyle, fillcolor);
  Fillpoly(numdat, polyVector );
END;


FUNCTION TRT.RTPowerCalc( realnum: REALTYPE;
                    power :  REALTYPE ):  REALTYPE;
BEGIN
  RTPowerCalc := exp(power * ln(abs(realnum)+1.0E-20));
END;


FUNCTION TRT.RTlog( realnum:  REALTYPE) :  REALTYPE;
BEGIN
  IF realnum < 1.0e-20 THEN
    RTlog := -16.0
  ELSE
    RTlog := ln(realnum) / 2.302585;
END;


PROCEDURE TRT. RTrswap( VAR xx1,xx2:  REALTYPE);
 VAR
   temp : REALTYPE;
BEGIN
  temp := xx2;
  xx2 := xx1;
  xx1 := temp;
END;

PROCEDURE TRT. RTiswap( VAR x1,x2:  INTEGER);
VAR
   temp : INTEGER;
BEGIN
  temp := x2;
  x2 := x1;
  x1 := temp;
END;


FUNCTION TRT.RTNumExp( realnum :  REALTYPE ): INTEGER;
BEGIN
  RTNumExp := trunc(RTlog(abs(realnum)));
END;


PROCEDURE TRT. RTFindMinMax(dataset : TPOINTER_C;
                          numdat  :  WORD;
                      VAR minval  :  REALTYPE;
                      VAR maxval  :  REALTYPE);

VAR
  i : INTEGER;
  v: REALTYPE;
BEGIN
  minval := dataset.GetPntrReal(0);
  maxval := minval;
  FOR i := 0 TO numdat - 1 DO
  BEGIN
    v := dataset.GetPntrReal(i);
    IF v < minval THEN minval := v;
    IF v > maxval THEN maxval := v;
  END;
END;


PROCEDURE TRT.RTSortData( VAR x,y : TPOINTER_C;
                        n,d :  INTEGER);
  VAR
     i, j, kk : INTEGER;
     TempX, TempY : REALTYPE;
     TempArray : TPOINTER_C;
     abort : BOOLEAN;

BEGIN
  IF( MaxAvail > (n*sizeof(REALTYPE))) THEN
    abort := TempArray.Creer(n);
    if not(abort) then begin
    write('RTGSUBS TempArray Overflow');
    halt;
    end
  ELSE        
  BEGIN
    write('RTGSUBS Heap Overflow ');
    halt;
  END;
  IF n > 1 THEN
  BEGIN
    FOR j := 0 TO n - 1 DO
    BEGIN
      abort := FALSE;
      TempX := x.GetPntrReal(j);
      TempY := y.GetPntrReal(j);
      k := j - 1;
      WHILE NOT (abort) AND (kk >= 0) DO
        IF TempX < x.GetPntrReal(kk) THEN
        BEGIN
          x.PutPntrReal(kk + 1,x.GetPntrReal(kk));
          y.PutPntrReal(kk + 1,y.GetPntrReal(kk));
          kk := kk - 1;
        END
        ELSE
          abort := TRUE;
      x.PutPntrReal(kk + 1,TempX);
      y.PutPntrReal(kk + 1,TempY);
    END;             

    IF (d = 0) THEN
    BEGIN
      for i := 1 to n do begin
        TempX := x.GetPntrReal(i); TempArray.PutPntrReal(i, TempX);
      end;
      FOR j := n - 1 DOWNTO 0 DO
      x.PutPntrReal(j, TempArray.GetPntrReal(n - 1 - j));
      FOR j := 0 to n-1 do
      TempArray.PutPntrReal(j, y.GetPntrReal(j));
      FOR j := n - 1 DOWNTO 0 DO
      y.PutPntrReal(j, TempArray.GetPntrReal(n - 1 - j));
    END;
  END;
  TempArray.Detruire;
END;


PROCEDURE TRT.RTSortDataX( VAR x,y :  TPOINTER_C;
                         n,d :  INTEGER);
BEGIN
  RTSortData(x, y, n, d);
END;


PROCEDURE TRT.RTSortDataY( VAR x, y:  TPOINTER_C;
                         n, d:  INTEGER );
BEGIN
  RTSortData(y, x, n, d);
END;

FUNCTION TRT.RTWinPWidth(win: RTStatPntr): INTEGER;
BEGIN
   RTWinPWidth := win^.dr.right - win^.dr.left;
END;

FUNCTION TRT.RTWinWidth(win: RTStatPntr): REALTYPE;
BEGIN
   RTWinWidth := win^.pw.right - win^.pw.left;
END;

FUNCTION TRT.RTWinPHeight(win: RTStatPntr): INTEGER;
BEGIN
   RTWinPHeight := abs(win^.dr.bottom - win^.dr.top);
END;

FUNCTION TRT.RTWinWHeight(win: RTStatPntr): REALTYPE;
BEGIN
   RTWinWHeight := win^.pw.top - win^.pw.bottom;
END;

FUNCTION TRT.RTPlotAreaPWidth(win: RTStatPntr): INTEGER;
BEGIN
   RTPlotAreaPWidth := win^.pr.right - win^.pr.left;
END;

FUNCTION TRT.RTPlotAreaWWidth(win: RTStatPntr): REALTYPE;
BEGIN
   RTPlotAreaWWidth := win^.pc.right - win^.pc.left;
END;

FUNCTION TRT.RTPlotAreaPHeight(win: RTStatPntr): INTEGER;
BEGIN
   RTPlotAreaPHeight := abs(win^.pr.bottom - win^.pr.top);
END;

FUNCTION TRT.RTPlotAreaWHeight(win: RTStatPntr): REALTYPE;
BEGIN
   RTPlotAreaWHeight := win^.pc.top - win^.pc.bottom;
END;

FUNCTION TRT.RTCalcTicXPix(majmin: INTEGER): INTEGER;
VAR pixels: INTEGER;
BEGIN
  IF majmin = 0 THEN
    pixels := getmaxy div 150 ELSE pixels := getmaxy div 75;
  RTCalcTicXPix := pixels;
END;

FUNCTION TRT.RTCalcTicYPix(majmin: INTEGER): INTEGER;
VAR pixels: INTEGER;
BEGIN
  IF majmin = 0 THEN
    pixels := getmaxx div 200  ELSE pixels := getmaxx div 100 ;
  RTCalcTicYPix := pixels;
END;

FUNCTION TRT.RTCalcTicX(win: RTStatPntr; majmin: INTEGER): REALTYPE;
VAR pixels: INTEGER;
    _result: REALTYPE;
BEGIN
  pixels := RTCalcTicXPix(majmin);
  _result := (RTWinWHeight(win)*pixels) /(RTWinPHeight(win));
  RTCalcTicX := _result;
END;

FUNCTION TRT.RTCalcTicY(win: RTStatPntr; majmin: INTEGER): REALTYPE;
VAR pixels: INTEGER;
    _result: REALTYPE;
BEGIN
  pixels := RTCalcTicYPix(majmin);
  _result := RTWinWidth(win)*pixels /(RTWinPWidth(win));
  RTCalcTicY := _result;
END;

PROCEDURE TRT.RTSetWin2PlotRatio(win : RTStatPntr; l,t,r,b: REALTYPE);
BEGIN
  win^.w2pr.left := l;
  win^.w2pr.right  := r;
  win^.w2pr.top    := t;
  win^.w2pr.bottom := b;
  win^.pr.left  := win^.dr.left + round((RTWinPWidth(win)) * win^.w2pr.left);
  win^.pr.bottom:= win^.dr.bottom -
	  round((RTWinPHeight(win)) * win^.w2pr.bottom);
  win^.pr.right:= win^.dr.right - round((RTWinPWidth(win)) * win^.w2pr.right);
  win^.pr.top := win^.dr.top + round((RTWinPHeight(win)) * win^.w2pr.top);
END;

PROCEDURE TRT.RTSetRect( VAR r: rect; xx1,yy1,xx2,yy2 : INTEGER );
BEGIN
  r.left := xx1;
  r.top := yy1;
  r.right := xx2;
  r.bottom := yy2;
END;

PROCEDURE TRT. RTSDRVp(win: RTStatPntr);
BEGIN
  RTSetGraphViewPort( win^.dr.left, win^.dr.top, win^.dr.right, win^.dr.bottom );
END;

PROCEDURE TRT. RTSPRVp(win: RTStatPntr);
BEGIN
  RTSetGraphViewPort( win^.pr.left, win^.pr.top, win^.pr.right, win^.pr.bottom );
END;

PROCEDURE TRT. RTSPCW(win: RTStatPntr);
BEGIN
  RTSetGraphViewport(win^.pr.left, win^.pr.top,
  win^.pr.right, win^.pr.bottom);
  RTSetWorldCoordinates(win^.pc.left, win^.pc.bottom,win^.pc.right,
                        win^.pc.top);
END;

PROCEDURE TRT. RTSPWW(win: RTStatPntr);
BEGIN
  RTSetGraphViewport(win^.dr.left, win^.dr.top ,win^.dr.right, win^.dr.bottom);
  RTSetWorldCoordinates(win^.pw.left, win^.pw.bottom,win^.pw.right,
                        win^.pw.top);
END;

FUNCTION TRT.RTCheckRange(minval,maxval, inval: REALTYPE): BOOLEAN;
VAR _result: BOOLEAN;
BEGIN
  IF (inval >= minval) and (inval <= maxval) THEN _result := TRUE ELSE
  _result := FALSE;
  RTCheckRange := _result;
END;

PROCEDURE TRT. RTTitleWin(win: RTStatPntr; title: string);
BEGIN
  RTSDRVp(win);
  RTMoveToXX(RTViewportWidth div 2, RTViewportHeight-rtth(0.30));
  SetTextJustify(1,2);
  RTOutTextNI(title);
  SetTextJustify(0,0);
END;

PROCEDURE TRT. RTSetWinTextStyle(win: RTStatPntr; font, size: INTEGER );
BEGIN
  win^.curfont := font;
  win^.curfontsize := size;
END;

(*
PROCEDURE TRT. RTUnitsWindow(win: RTStatPntr; units: string;
                            x,y,orient: INTEGER);
VAR  textInfo: TextSettingsType;
BEGIN
  gettextsettings(textinfo);  { BGI, get current text settings }
  settextstyle(textinfo.font,0,textinfo.charsize);  { BGI }
  rtsdrvp(win); { set to drawing rect }
  IF ( orient=1 ) THEN   { if vertical }
  BEGIN
    settextstyle(textinfo.font,1,textinfo.charsize);  { BGI, rotate font }
    IF (x < rtviewportwidth div 2) THEN
    BEGIN
       settextjustify(RIGHTTEXT,CENTERTEXT);   { BGI, right, center justify }
       x := x + RTTH(1.2);
    END
    ELSE
    BEGIN
      settextjustify(RIGHTTEXT,CENTERTEXT);   { BGI, right, center justify }
    END
  END
  ELSE
  BEGIN
     settextjustify(CENTERTEXT,BOTTOMTEXT); { BGI, center,bottom  justify }
  END;
  rtmovetoxx(x, y); { move to adjusted position }
  rtouttextni(units); { output units string }
  if ( orient = 1 ) THEN { return text settings to original values }
    settextstyle(textinfo.font,0,textinfo.charsize);  { BGI }
  settextjustify(LEFTTEXT,BOTTOMTEXT);  { BGI }
END;

PROCEDURE TRT. RTPIDTagWindow(win: RTStatPntr; tags: tagarraytype; y1,n: INTEGER);
var i,x1: INTEGER;
BEGIN
  RTSDRVp(win);
  x1 :=  RTViewportWidth div (6);
  SetTextJustify(1,0);
  for i := 0 to n-1 do
  begin
    RTSetColor(win^.linecolor[i]);
    RTMoveToXX(x1,y1);
    RTOutTextNI(tags[i]);
    y1 := y1 - RTTH(1.25);
  end;
  SetTextJustify(0,0);
END;

PROCEDURE TRT. RTHTagWindow(win: RTStatPntr; tags: tagarraytype; y1,n: INTEGER);
var i,x1: INTEGER;
BEGIN
  RTSDRVp(win);
  x1 :=  RTViewportWidth div (2*n);
  SetTextJustify(1,2);
  for i := 0 to n-1 do
  begin
    RTSetColor(win^.linecolor[i]);
    RTMoveToXX(x1,y1);
    RTOutTextNI(tags[i]);
    x1 := x1 + RTViewportWidth div n;
  end;
  SetTextJustify(0,0);
END;

PROCEDURE TRT.RTVTagWindow(win: RTStatPntr; tags: tagarraytype; n,side: INTEGER);
var i,x1,y1,sy: INTEGER;
    vphr: RealType;
BEGIN
  RTSetGraphViewPort( win^.dr.left, win^.pr.top,
                    win^.dr.right, win^.pr.bottom );
  CASE side of
    0: BEGIN
       x1 := 2;
       SetTextJustify(0,1);
     END;
  1: BEGIN
       x1 :=  RTViewportWidth -2;
       SetTextJustify(2,1)
     END;
  END;
  vphr := RTViewportHeight;
  sy :=  round(vphr - vphr / (2*n));
  for i := 0 to n-1 do
  begin
    y1 := sy - round((i) * (vphr / n));
    RTSetColor(win^.lineColor[i]);
    RTMoveToXX(x1,y1);
    RTOutTextNI(tags[i]);
  end;
  RTSDRVp(win);
  SetTextJustify(0,0);
END;
(*
FUNCTION TRT.RTGetHTagChannel(win: RTStatPntr; xp,yp: INTEGER): INTEGER;
VAR nt,th,delta,deltadiv2,result,xvp, yvp,i,x1,y1,_vph,_vpw: INTEGER;
BEGIN
  RTSDRVp(win);
  _vpw := RTViewportWidth;
  _vph := RTViewportHeight;
  SetTextStyle(win^.curFont,0,win^.curFontSize);
  nt := win^.numtraces;
  x1 :=  _vpw div (2*nt);
  delta := _vpw div nt;
  deltadiv2 := delta div 2;
  th := TextHeight('1');
  y1 := _vph -2*th +2;
  for i := 0 to nt-1 do
  begin
    RTPixToViewport(win, 0,xp,yp, xvp,yvp);
    IF RTInRegion(xvp,yvp,x1-deltadiv2,_vph-y1,
                  x1 + deltadiv2,_vph-y1+th)
    THEN
    BEGIN
      result := i;
      RTGetHTagChannel := result;
      exit;
    END;
    x1 := x1 + delta;
  end;
  result := -1;
  RTGetHTagChannel := result;
END;

FUNCTION TRT.RTGetVTagChannel(win: RTStatPntr; xp,yp,side: INTEGER): INTEGER;
var th,n,delta,result,_vpw,_vph,xvp,yvp,w,i,x1,y1: INTEGER;
BEGIN
  RTSDRVp(win);
  _vpw := RTViewportWidth;
  _vph := win^.pr.bottom - win^.pr.top;
  n := win^.numtraces;
  delta := _vph div n;
  y1 :=  win^.pr.top- win^.dr.top;
  IF side = 1 THEN
    w := _vpw ELSE
  w := win^.pr.left - win^.dr.left;
  for i := 0 to n-1 do
  begin
    RTPixToViewport(win, 0,xp,yp, xvp,yvp);
    IF RTInRegion(xvp,yvp,0,y1,w,y1+delta)
    THEN
    BEGIN
      result := i;
      RTGetVTagChannel := result;
      exit;
    END;
    y1 := y1 + delta;
  end;
  result := -1;
  RTGetVTagChannel := result;
END;

FUNCTION TRT.RTCalcTagWidth(tags: tagArrayType; n: INTEGER): INTEGER;
VAR result,index,l,i: INTEGER;
BEGIN
  result := length(tags[0]);
  index := 0;
  for i := 1 to n-1 do
  begin
    l := length(tags[i]);
    IF l > result THEN
    begin
      result := l;
      index := i;
    end;
  END;
  RTCalcTagWidth := TextWidth(tags[index]);
END;
*)

FUNCTION TRT.RTCalcLabelWidth(minVal, maxVal: REALTYPE; decimals: INTEGER): INTEGER;
VAR _result,maxl,lmin,lmax: INTEGER;
    minStr: string absolute strw;
    maxStr: string;
BEGIN
  str(minval:1:decimals,minStr);
  str(maxval:1:decimals,maxStr);
  lmin := length(minstr);
  lmax := length(maxstr);
  IF lmax >= lmin THEN _result := TextWidth(maxstr) else
  _result := TextWidth(minstr);
  RTCalcLabelWidth := _result;
END;
(*
PROCEDURE TRT.RTLineRatchetNI(x1,y1,x2,y2,mode: INTEGER);
BEGIN
  RTIntrOff;
  IF (mode=0) THEN
  BEGIN
    Line(x1,y1,x1,y2);
    Line(x1,y2,x2,y2);
  END
  ELSE
  BEGIN
    Line(x1,y1,x2,y1);
    Line(x2,y1,x2,y2);
  END;
  RTIntrOn;
END;

PROCEDURE TRT.RTDrawRatchetPoly(n:word; data: RTBufferArrayType; mode: INTEGER);
VAR x1,y1,x2,y2,i: INTEGER;
BEGIN
  x1 := data[0].x;
  y1 := data[0].y;
  _MoveTo(x1,y1);
  for i := 1 to n-1 do
  BEGIN
    x2 := data[i].x;
    y2 := data[i].y;
    RTLineRatchetNI(x1,y1,x2,y2, mode);
    x1 := x2; y1 := y2;
  END;
END;

PROCEDURE TRT.RTDrawAlarmLine(min, max, alarmval: REALTYPE;
                               hvmode, color: INTEGER);
BEGIN
    IF (hvmode = 0) THEN
    BEGIN
         RTSetColor(color);
         RTMoveWorldAbs(min,alarmVal);
         RTLineWorldAbs(max,alarmVal);
    END
    ELSE
    BEGIN
         RTSetColor(color);
         RTMoveWorldAbs(alarmVal,min);
         RTLineWorldAbs(alarmVal,max);
    END;
END;
*)
FUNCTION TRT.RTCalcYLabTics(min,max: INTEGER): INTEGER;
VAR w,_result : INTEGER;
BEGIN
  w := abs(max-min);
  CASE w OF
  0..50: _result := 5;
  51..100: _result := 10;
  101..200: _result := 20;
  201..480: _result := 50;
  ELSE _result := 100;
  END;
  RTCalcYLabTics := _Result;
END;

FUNCTION TRT.RTInRegion(x,y, x1,y1,x2,y2: INTEGER): BOOLEAN;
VAR _result: BOOLEAN;
BEGIN
  IF y1 > y2 THEN RTiswap(y1,y2);
  IF (x > x1) and ( x< x2) and (y > y1) and (y < y2) THEN
    _result := TRUE
  ELSE
    _result := FALSE;
  RTInRegion := _result;
END;

FUNCTION TRT.RTFindWindow(win: RTStatPntr; x,y: INTEGER): BOOLEAN;
VAR w,i,j: integer;
    _result: BOOLEAN;
BEGIN
  _result := FALSE;
  IF win^.valid THEN
  BEGIN
    IF RTInRegion(x,y,win^.dr.left,win^.dr.top,win^.dr.right,win^.dr.bottom)
    THEN _result := TRUE;
  END;
  RTFindWindow := _result;
END;


PROCEDURE TRT.RTSetGraphAreaWorld(win: RTStatPntr; a,b,c,d : REALTYPE);
BEGIN
  win^.pw.left := a - (c - a) *  (win^.w2pr.left/(1.0-(win^.w2pr.left+win^.w2pr.right)));
  win^.pw.bottom := b - (d - b) * (win^.w2pr.bottom/(1.0-(win^.w2pr.bottom+win^.w2pr.top)));
  win^.pw.right := c + (c - a)*  (win^.w2pr.right/(1.0-(win^.w2pr.right+win^.w2pr.left)));
  win^.pw.top := d + (d - b) *   (win^.w2pr.top/(1.0-(win^.w2pr.top+win^.w2pr.bottom)));
  RTSetWorldRect(win^.pc, a, b, c, d);
END;


PROCEDURE TRT.RTSetWindow(win : RTStatPntr );
BEGIN
  RTSDRVp(win);
END;

PROCEDURE TRT.RTDefGraphWindow(win: RTStatPntr; xx1,yy1,xx2,yy2 :  INTEGER);
BEGIN
  RTSetRect( win^.dr, xx1, yy1, xx2, yy2);
  win^.valid := TRUE;
  win^.pr.left  := win^.dr.left +
           trunc((RTWinPWidth(win)) * win^.w2pr.left);
  win^.pr.bottom:= win^.dr.bottom -
         trunc((RTWinPHeight(win)) * win^.w2pr.bottom);
  win^.pr.right := win^.dr.right -
          trunc((RTWinPWidth(win)) * win^.w2pr.right);
  win^.pr.top   := win^.dr.top +
            trunc((RTWinPHeight(win)) * win^.w2pr.top);
  RTSetWorldRect( win^.pc, 0.0, 0.0, 1000.0, 1000.0);
  RTSetWorldRect(win^.pw,0.0, 0.0, 1000.0, 1000.0);
  RTSetGraphAreaWorld(win,0.0, 0.0, 1000.0, 1000.0);
  RTSetGraphViewport(xx1,yy1,xx2,yy2);
 END;


PROCEDURE TRT. RTBorderWindow(win: RTStatPntr; c: integer);
BEGIN
  RTSDRVp(win);
  RTSetColor(c);
  SetLineStyle(0,0,1);
  RTRectangleXX( 0,0,RTViewportWidth,RTViewportheight);
  win^.borderflag := 1;
  win^.bordercolor := c;
END;

PROCEDURE TRT. RTSetViewBackground(win: RTStatPntr; c: integer);
VAR dr: rect;
    f: INTEGER;
BEGIN
  win^.vbgcolor := c;
  RTSetColor(c);
  SetFillStyle(1,c);
  dr := win^.dr;
  RTSDRVp(win);
  IF (NOT((rtcheckprnon) AND (c=0))) THEN
    RTbarXX( 0,0,(win^.dr.right-win^.dr.left),(win^.dr.bottom-win^.dr.top));
END;

PROCEDURE TRT. RTSetPlotBackground(win: RTStatPntr; c:integer);
VAR f: INTEGER;
BEGIN
  win^.pbgcolor := c;
  SetFillStyle(1,c);
  RTSetColor(c);
  RTSPRVp(win);
  IF (( win^.vbgcolor > 0) OR NOT((rtcheckprnon) AND (c=0))) THEN
    RTbarxx(0,0,win^.pr.right-win^.pr.left,win^.pr.bottom-win^.pr.top);
  RTSDRVp(win);
END;

PROCEDURE TRT. RTSetAxisDirection(win: RTStatPntr; axdir: INTEGER);
BEGIN
  win^.hvflag := axdir;
END;

PROCEDURE TRT. RTScalePlotArea(win: RTStatPntr; xx1,yy1,xx2,yy2 :  REALTYPE );
BEGIN
  RTSetGraphAreaWorld(win,xx1, yy1, xx2, yy2);
  if (xx2 > xx1) THEN
  BEGIN
    win^.xscaledir := 0;
    win^.mm.left := xx1;
    win^.mm.right := xx2;
  END
  else
  BEGIN
     win^.xscaledir := 1;
     win^.mm.left := xx2;
     win^.mm.right := xx1;
  END;
  if (yy2 > yy1) THEN
  BEGIN
    win^.yscaledir := 0;
    win^.mm.bottom := yy1;
    win^.mm.top := yy2;
  END
  else
  BEGIN
    win^.yscaledir := 1;
    win^.mm.bottom := yy2;
    win^.mm.top := yy1;
  END;
END;


PROCEDURE TRT. RTClearWindow(win: RTStatPntr);
BEGIN
  RTSDRVp(win);
  ClearViewport;
END;

PROCEDURE TRT.RTClearGraph(win: RTStatPntr);
BEGIN
  RTSetGraphViewport(win^.pr.left+1, win^.pr.top+1 ,win^.pr.right-1, win^.pr.bottom-1);
  ClearViewport;
  RTSDRVp(win);
END;

PROCEDURE TRT.RTSetXYIntercepts(win: RTStatPntr;  xx1,yy1:  REALTYPE);
BEGIN
  win^.xint := xx1;
  win^.yint := yy1;
END;


PROCEDURE TRT.RTDrawTicX(win: RTStatPntr;  x,y,l :  REALTYPE );
BEGIN
  IF (x <= win^.mm.right) AND (x >= win^.mm.left) THEN
  BEGIN
    RTMoveWorldAbs(x, y);
    RTLineWorldAbs(x, y - l);
  END;
END;


PROCEDURE TRT.RTDrawTicY(win: RTStatPntr;  x,y,l:  REALTYPE);
BEGIN
  IF (y <= win^.mm.top) AND (y >= win^.mm.bottom) THEN
  BEGIN
    RTMoveWorldAbs(x, y);
    RTLineWorldAbs(x - l, y);
  END;
END;

PROCEDURE TRT. RTDrawXAxis(win: RTStatPntr; TicSpace:  REALTYPE;
                   dir : INTEGER);
VAR
    tl, xx1, ticlen : REALTYPE;
BEGIN
  RTSPWW(win);
  ticspace := abs(ticspace);
  RTMoveWorldAbs((win^.pc.left), (win^.yint));
  RTLineWorldAbs((win^.pc.right), (win^.yint));
  win^.numticx := trunc((RTPlotAreaWWidth(win)) / TicSpace);
  win^.ticspacex := TicSpace;
  xx1 := win^.xint;
  tl := RTCalcTicX(win,0);
  IF dir = 0 THEN
    ticlen := tl
  ELSE
    ticlen := -tl;
  WHILE xx1 <= win^.mm.right DO
  BEGIN
    RTDrawTicX(win,xx1, win^.yint, ticlen);
    xx1 := xx1 + win^.ticspacex;
  END;
  xx1 := win^.xint - win^.ticspacex;
  WHILE xx1 >= win^.mm.left DO
  BEGIN
    RTDrawTicX(win,xx1, win^.yint, ticlen);
    xx1 := xx1 - win^.ticspacex;
  END;
END;


PROCEDURE TRT.RTDrawYAxis(win: RTStatPntr; TicSpace:  REALTYPE;
                   dir : INTEGER);
  VAR
    tl, yy1, ticlen : REALTYPE;
BEGIN
  RTSPWW(win);
  ticspace := abs(ticspace);
  RTMoveWorldAbs((win^.xint), (win^.pc.bottom));
  RTLineWorldAbs((win^.xint), (win^.pc.top));
  win^.numticy := trunc((RTPlotAreaWHeight(win)) / TicSpace);
  win^.ticspacey := TicSpace;
  yy1 := win^.yint;
  tl := RTCalcTicY(win,0);
  IF dir = 0 THEN
    ticlen := tl
  ELSE
    ticlen := -tl;
  WHILE yy1 <= win^.mm.top DO
  BEGIN
    RTDrawTicY(win, win^.xint, yy1, ticlen);
    yy1 := yy1 + win^.ticspacey;
  END;
  yy1 := win^.yint - win^.ticspacey;
  WHILE yy1 >= win^.mm.bottom DO
  BEGIN
    RTDrawTicY(win,win^.xint, yy1, ticlen);
    yy1 := yy1 - win^.ticspacey;
  END;
END;

FUNCTION TRT.RTCalcDecs(a1,a2 :  REALTYPE):INTEGER;
VAR
   n1, n2,_result : INTEGER;
BEGIN
  IF abs(a2) >= abs(a1) THEN
    n1 := RTNumExp(a2)
  ELSE
    n1 := RTNumExp(a1);
  _result := 0;
  IF (n1 < 3) THEN
  BEGIN
    IF (n1 > -2 ) THEN _result := abs(n1-2)
    ELSE
      _result := -1;
  END ELSE IF (n1 < 5) THEN _result := 0 ELSE _result := -1;
  RTCalcDecs := _result;
END;


PROCEDURE TRT.RTLabelTicYString(win: RTStatPntr;  xx, yy  :  REALTYPE;
                           TicLabel: string;
                           dir     :  INTEGER );
VAR
    tl, xx1, yy1 : REALTYPE;
BEGIN
   IF ((yy <= win^.mm.top+win^.ticspacey/10.0) and
     (yy >= win^.mm.bottom-+win^.ticspacey/10.0)) THEN
   BEGIN
     tl := RTCalcTicY(win,1);
     IF dir = 0 THEN
       RTDrawTicY(win,xx, yy, tl)
     ELSE
       RTDrawTicY(win,xx, yy, -tl);
     yy1 := yy;
     IF dir = 0 THEN
       xx1 := xx - 1.2 * tl
     ELSE
       xx1 := xx + 1.5 * tl;
     RTMoveWorldAbs(xx1, yy1);
     OutText( TicLabel);
   END;
END;


PROCEDURE TRT.RTLabelTicY(win: RTStatPntr; x, y, yval:  REALTYPE;
                     decs,dir:  INTEGER );
VAR
    labvalstr : string absolute strw;
BEGIN
  Str(yval:1:decs, labvalstr);
  RTLabelTicYString(win,x, y, labvalstr, dir);
END;


PROCEDURE TRT.RTLabelYAxis(win: RTStatPntr; NthTic, decs, dir:  INTEGER);
VAR
   yy1 : REALTYPE;
BEGIN
  RTSPWW(win);
  IF dir = 0 THEN SetTextJustify( RightText, CenterText )
  Else
    SetTextJustify( LeftText, CenterText );
  win^.tsy := win^.ticspacey * NthTic;
  yy1 := win^.yint;
  IF dir = 0 THEN
  begin
    IF win^.xint <>  win^.pc.left THEN yy1 := yy1 + win^.tsy;
  end
  else
    IF win^.xint <>  win^.pc.right THEN yy1 := yy1 + win^.tsy;
  WHILE yy1 <= (win^.mm.top+win^.ticspacey/10.0) DO
  BEGIN
    RTLabelTicY(win,win^.xint, yy1, yy1, decs,dir);
    yy1 := yy1 + win^.tsy;
  END;
  yy1 := win^.yint - win^.tsy;
  WHILE yy1 >= (win^.mm.bottom-win^.ticspacey/10.0) DO
  BEGIN
    RTLabelTicY(win,win^.xint, yy1, yy1, decs,dir);
    yy1 := yy1 - win^.tsy;
  END;
  SetTextJustify( LeftText, CenterText );
END;

PROCEDURE TRT.RTLableTicXString(win: RTStatPntr; x, y :  REALTYPE;
                           TicLabel : string;
                           dir  :  INTEGER );
  VAR
    tl, xx1, yy1 : REALTYPE;
BEGIN
  IF (x <= win^.mm.right+win^.ticspacex/10.0)
  AND (x >= win^.mm.left-win^.ticspacex/10.0) THEN
  BEGIN
    tl := RTCalcTicX(win,1);
    IF dir = 0 THEN
      RTDrawTicX(win,x, y, tl)
    ELSE
      RTDrawTicX(win,x, y, -tl);
    IF dir = 0 THEN
      yy1 := y - 1.2 * tl
    ELSE
      yy1 := y + 1.2 * tl;
    xx1 := x ;
    RTMoveWorldAbs((xx1), (yy1));
    OutText(TicLabel);
  END;
END;

PROCEDURE TRT.RTLabelTicX(win: RTStatPntr; x, y, xval : REALTYPE;
                      decs, dir: INTEGER);
VAR
    labvalstr : string absolute strw;
BEGIN
  Str(xval:1:decs, labvalstr);
  RTLableTicXString(win,x, y, labvalstr, dir);
END;


PROCEDURE TRT. RTLabelXAxisMod(win: RTStatPntr; start: RealType;
                                NthTic, decs, dir:  INTEGER);
  VAR
    xx1 : REALTYPE;
BEGIN
  RTSPWW(win);
  IF dir = 0 THEN
    SetTextJustify( CenterText, TopText )
  ELSE
    SetTextJustify( CenterText, BottomText );
  win^.tsx := win^.ticspacex * NthTic;
  xx1 := win^.xint;
  IF dir = 0 THEN
  begin
    IF win^.yint <>  win^.pc.bottom THEN xx1 := xx1 + win^.tsx;
  end
  else
    IF win^.yint <>  win^.pc.top THEN xx1 := xx1 + win^.tsx;
  WHILE xx1 <= (win^.mm.right+win^.ticspacex/10.0) DO
  BEGIN
    RTLabelTicX(win,xx1, win^.yint, xx1+start, decs, dir );
    xx1 := xx1 + win^.tsx;
  END;
  xx1 := win^.xint - win^.tsx;
  WHILE xx1 >= (win^.mm.left-win^.ticspacex/10.0) DO
  BEGIN
    RTLabelTicX(win,xx1, win^.yint, xx1+start, decs, dir);
    xx1 := xx1 - win^.tsx;
  END;
  SetTextJustify( LeftText, CenterText );
END;

PROCEDURE TRT. RTLabelXAxis(win: RTStatPntr; NthTic,  decs, dir :  INTEGER);
  VAR
    xx1 : REALTYPE;
BEGIN
  RTSPWW(win);
  IF dir = 0 THEN
    SetTextJustify( CenterText, TopText )
  ELSE
    SetTextJustify( CenterText, BottomText );
  win^.tsx := win^.ticspacex * NthTic;
  xx1 := win^.xint;
  IF dir = 0 THEN
  begin
    IF win^.yint <>  win^.pc.bottom THEN xx1 := xx1 + win^.tsx;
  end
  else
    IF win^.yint <>  win^.pc.top THEN xx1 := xx1 + win^.tsx;
  WHILE xx1 <= (win^.mm.right+win^.ticspacex/10.0) DO
  BEGIN
    RTLabelTicX(win,xx1, win^.yint, xx1, decs, dir);
    xx1 := xx1 + win^.tsx;
  END;
  xx1 := win^.xint - win^.tsx;
  WHILE xx1 >= (win^.mm.left-win^.ticspacex/10.0) DO
  BEGIN
    RTLabelTicX(win,xx1, win^.yint, xx1, decs, dir);
    xx1 := xx1 - win^.tsx;
  END;
  SetTextJustify( LeftText, CenterText );
END;

PROCEDURE TRT. RTLabelXAxisRT(win: RTStatPntr; NthTic,  decs, dir :  INTEGER;
                         startv: RealType);
  VAR
    xx1 : REALTYPE;
BEGIN
  RTSPWW(win);
  IF dir = 0 THEN
    SetTextJustify( CenterText, TopText )
  ELSE
    SetTextJustify( CenterText, BottomText );
  win^.tsx := win^.ticspacex * NthTic;
  xx1 := win^.xint;
  IF dir = 0 THEN
  BEGIN
    IF win^.yint <>  win^.pc.bottom THEN xx1 := xx1 + win^.tsx;
  END
  ELSE
    IF win^.yint <>  win^.pc.top THEN xx1 := xx1 + win^.tsx;
  WHILE xx1 <= (win^.mm.right+win^.ticspacex/10.0) DO
  BEGIN
    RTLabelTicX(win,xx1, win^.yint, xx1+startv, decs, dir);
    xx1 := xx1 + win^.tsx;
  END;
  xx1 := win^.xint - win^.tsx;
  WHILE xx1 >= (win^.mm.left-win^.ticspacex/10.0) DO
  BEGIN
    RTLabelTicX(win,xx1, win^.yint, xx1+startv, decs, dir);
    xx1 := xx1 - win^.tsx;
  END;
  SetTextJustify( LeftText, CenterText );
END;

PROCEDURE TRT. RTLabelYAxisRT(win: RTStatPntr; NthTic,  decs, dir :  INTEGER;
                         startv: RealType);
  VAR
    yy1 : REALTYPE;
BEGIN
  RTSPWW(win);
  IF dir = 0 THEN
    SetTextJustify( RightText, CenterText )
  ELSE
    SetTextJustify( LeftText, CenterText );
  win^.tsy := win^.ticspacey * NthTic;
  yy1 := win^.yint;
  IF dir = 0 THEN
  BEGIN
    IF win^.xint <>  win^.pc.left THEN yy1 := yy1 + win^.tsy;
  END
  ELSE
    IF win^.xint <>  win^.pc.right THEN yy1 := yy1 + win^.tsy;
  WHILE yy1 <= (win^.mm.top+win^.ticspacey/10.0) DO
  BEGIN
    RTLabelTicY(win, win^.xint, yy1, yy1+startv, decs, dir);
    yy1 := yy1 + win^.tsy;
  END;
  yy1 := win^.yint - win^.tsy;
  WHILE yy1 >= (win^.mm.bottom-win^.ticspacey/10.0) DO
  BEGIN
    RTLabelTicY(win, win^.xint, yy1, yy1+startv, decs, dir);
    yy1 := yy1 - win^.tsy;
  END;
  SetTextJustify( LeftText, CenterText );
END;


PROCEDURE TRT. RTDrawGridX(win: RTStatPntr; NthTic :  INTEGER);
VAR
  xx1, xx2, yy1, yy2 : REALTYPE;
BEGIN
  RTSPWW(win);
  win^.tsx := win^.ticspacex * NthTic;
  xx1 := win^.xint + win^.tsx;
  yy1 := win^.pc.bottom;
  yy2 := win^.pc.top;
  WHILE xx1 < win^.mm.right DO
  BEGIN
    RTMoveWorldAbs(xx1, yy1);
    RTLineWorldAbs(xx1, yy2);
    xx1 := xx1 + win^.tsx;
  END;
  xx1 := win^.xint;
  WHILE xx1 > win^.mm.left DO
  BEGIN
    RTMoveWorldAbs(xx1, yy1);
    RTLineWorldAbs(xx1, yy2);
    xx1 := xx1 - win^.tsx;
  END;
  RTSPCW(win);
END;


PROCEDURE TRT. RTDrawGridY(win: RTStatPntr;  NthTic :  INTEGER);
VAR
  xx1, xx2, yy1, yy2 : REALTYPE;
BEGIN
  RTSPWW(win);
  win^.tsy := win^.ticspacey * NthTic;
  xx1 := win^.pc.left;
  xx2 := win^.pc.right;
  yy1 := win^.yint;
  WHILE yy1 < win^.mm.top DO
  BEGIN
    RTMoveWorldAbs(xx1, yy1);
    RTLineWorldAbs(xx2, yy1);
    yy1 := yy1 + win^.tsy;
  END;
  yy1 := win^.yint;
  WHILE yy1 > win^.mm.bottom DO
  BEGIN
    RTMoveWorldAbs(xx1, yy1);
    RTLineWorldAbs(xx2, yy1);
    yy1 := yy1 - win^.tsy;
  END;
  RTSPCW(win);
END;

PROCEDURE TRT. RTDrawGrid(win: RTStatPntr; NthTic :  INTEGER);
BEGIN
  RTDrawGridY(win,NthTic);
  RTDrawGridX(win,NthTic);
END;

FUNCTION Sign(i: REALTYPE): REALTYPE;
BEGIN
  IF i > 0 THEN sign := 1 else IF i < 0 THEN sign := -1 else sign := 0;
END;


PROCEDURE TRT. RTInitWindowColors(win: RTStatPntr; pbgcol,
                              vbgCol, lacol, hacol,
                             okcol,labcol,axiscol: INTEGER);
BEGIN

  win^.pbgcolor := pbgcol;
  win^.vbgColor := vbgCol;
  IF maxcolor < 3 THEN
  BEGIN
    win^.pbgcolor := 0;
    win^.vbgColor := 0;
  END;
  win^.hacolor := hacol;
  win^.lacolor := lacol;
  win^.okcolor := okcol;
  win^.labcolor := labcol;
  win^.axiscolor := axiscol;
END;

PROCEDURE TRT. SetRTComVars(win: RTStatPntr;
	       RTType: RTGraphType;
	       lcolor,
	       lfill: RTIntArrayType;
	       titstr: titleType;
	       tags: tagArrayType;
	       Eunits: tagType;
	       numT: INTEGER;
	       xdecs, ydecs: INTEGER);
VAR i: INTEGER;
BEGIN
  win^.RTWinType := RTType;
  win^.linecolor := lcolor;
  win^.linefill :=  lfill;
  win^.titlestr := titstr;
  win^.eunitstr := eunits;
  win^.tagstrs := tags;
  win^.numtraces := numt;
  win^.xdecimals := xdecs;
  win^.ydecimals := ydecs;
  win^.rtfirstpass := TRUE;
END;
(*
FUNCTION TRT.RTCalcAlarmState(la,ha,cv: REALTYPE): INTEGER;
VAR result: INTEGER;
BEGIN
  IF (cv < la) THEN result := 0
    ELSE IF (cv > ha) THEN result := 2
      ELSE result := 1;
  RTCalcAlarmState := result;
END;

FUNCTION TRT.RTDeltaBar(old, new, center: REALTYPE): INTEGER;
VAR result: INTEGER;
BEGIN
   IF (old >= center) AND (new >= center) THEN result := 0
   ELSE
     IF (old <= center) AND (new <= center) THEN result := 1
   ELSE
     IF ((old >= center) AND (new <= center)) OR
       (( old <= center) AND (new >= center)) THEN
       result := 2
   ELSE result := 0;
   RTDeltaBar := result;
END;
*)
PROCEDURE TRT. RTWriteIText(x,y: INTEGER;
                       lastVal, newVal: REALTYPE; fc,bc,decimals: INTEGER);
VAR  lastrtstr, rtstr: string;
BEGIN
  str(lastVal:2:decimals,lastrtstr);
  str(newVal:2:decimals,rtstr);
  RTSetColor(bc);
  RTMoveToXX(x,y);
  RTOutTextNI(lastrtstr);
  RTSetColor(fc);
  RTMoveToXX(x,y);
  RTOutTextNI(rtstr);
END;

PROCEDURE TRT. RTWriteStr(x,y: INTEGER;
                     oldStr, newStr: string; fc,bc: INTEGER);
BEGIN
  RTSetColor(bc);
  RTMoveToXX(x,y);
  RTOutTextNI(oldstr);
  RTSetColor(fc);
  RTMoveToXX(x,y);
  RTOutTextNI(newstr);
END;
(*
FUNCTION TRT.RTCalcAlarmColor(win: RTStatPntr; alarmState: INTEGER): INTEGER;
VAR result: INTEGER;
BEGIN
  CASE alarmState OF
  0: result := win^.lacolor;
  1: result := win^.okcolor;
  2: result := win^.hacolor;
  END;
  RTCalcAlarmColor := result;
END;
*)
PROCEDURE TRT. RTSetPercentWindow(win: RTStatPntr;  x1,y1,x2,y2 : REALTYPE);
var maxX, maxY: INTEGER;
BEGIN
  rtsetworldrect(win^.nwp,x1,y1,x2,y2);
  RTGetMaxCoords(maxX, maxY);
  RTDefGraphWindow(win, trunc(x1*maxX),trunc(y1*maxY),trunc(x2*maxX),trunc(y2*maxY));
END;

PROCEDURE TRT. RTPixToWorld(win: RTStatPntr; xp,yp: INTEGER; VAR xw,yw: REALTYPE);
VAR  dvpx,dvpy,x1,y1,w: INTEGER;
     dwx, dwy: REALTYPE;
     winfound: BOOLEAN;
BEGIN
  winfound := RTFindWindow(win,xp,yp);
  IF winfound THEN
  BEGIN
    x1 := xp - win^.dr.left;
    y1 := win^.dr.bottom - yp;
    xw := win^.pw.left + x1 * RTWinWidth(win)/RTWinPWidth(win);
    yw := win^.pw.bottom + y1 * RTWinWHeight(win) / RTWinPHeight(win);
  END
  ELSE
  BEGIN
    xw := 0.0;
    yw := 0.0;
  END;
END;

PROCEDURE TRT. RTPixToViewport(win: RTStatPntr; drpr,xp,yp:
                          INTEGER; VAR xvp,yvp: INTEGER);
VAR  winfound: BOOLEAN;
     x1,y1,x2,y2: INTEGER;
BEGIN
  winfound := RTFindWindow(win,xp,yp);
  IF winfound THEN
  BEGIN
    IF drpr = 0 THEN BEGIN xvp := xp - win^.dr.left; yvp := yp-win^.dr.top; END
    ELSE
    BEGIN xvp := xp - win^.pr.left; yvp := yp-win^.pr.top; END
  END
END;

PROCEDURE TRT.rtsetrealbufferflag(win: rtstatpntr;  f: INTEGER);
BEGIN END;


PROCEDURE TRT.RTInitWindow(VAR win: RTStatPntr);
VAR i: INTEGER;
BEGIN
  IF MaxAvail > (sizeof(RTStatType)) THEN
  BEGIN
    NEW(win);
    FillChar(win^,sizeof(RTStatType),0);
  END
  ELSE
  BEGIN
    CloseGraph;
    write('RTInitWindow Heap Overflow');
   { repeat until keypressed; }
    halt;
  END;
  win^.RTWinType := NotRT;
  win^.valid := FALSE;
  RTSetWorldRect( win^.w2pr,0.166,0.166,0.166,0.166);
  RTSetPercentWindow(win,0.01,0.01,0.99,0.99);
  RTSetWin2PlotRatio(win,0.15,0.12,0.10,0.15);
  RTInitWindowColors(win,0,0,10,12,15,15,15);
  RTSetWinTextStyle(win,0,1);
  win^.borderflag := 0;
  win^.bordercolor := 0;
END;

{ Ancien RTInitGraphics }
Constructor TRT.Init( numwin, dowinint, gmaxx, gmaxy: INTEGER);
VAR i : integer;
BEGIN
  TObject.Init;
  GetMaxX := gmaxx; GetMaxY := gmaxy;
  Metafile := False;

   vpl := 640; vph := 480;
   RTClipOn;
   GetMem(rtstat, SizeOf(rtstatpntr) * MaxWin);

  ginit := 0;
  RTOneTimeInit;
  IF dowinint = 1 THEN
  BEGIN
    IF  numwin > MaxWin THEN i := MaxWin else IF numwin < 0 THEN numwin := 0;
    rtnumwin := numwin;
    FOR i := 0 to rtnumwin-1 do
      RTInitWindow(rtstat^[i]);
  END;

  with WinLogFont do
  begin
    lfHeight        := 12;
    lfWidth         := 8;
    lfEscapement    := 0;
    lfOrientation   := 0;
    lfWeight        := fw_Normal;
    lfItalic        := 0;
    lfUnderline     := 0;
    lfStrikeOut     := 0;
    lfCharSet       := ANSI_CharSet;
    lfOutPrecision  := Out_Default_Precis;
    lfClipPrecision := Clip_Default_Precis;
    lfQuality       := Proof_Quality;
    lfPitchAndFamily:= Variable_Pitch or FF_Roman;
    StrCopy(lfFaceName,'Times New Roman');
  end;

  BasicColors[ 0] :=  RGB(   0,   0,   0); { Black }
  BasicColors[ 1] :=  RGB(   0,   0, 127); { Dark blue }
  BasicColors[ 2] :=  RGB(   0, 127,   0); { Dark green }
  BasicColors[ 3] :=  RGB(   0, 127, 127); { Dark cyan }
  BasicColors[ 4] :=  RGB( 127,   0,   0); { Dark red }
  BasicColors[ 5] :=  RGB( 127,   0, 127); { Dark magenta }
  BasicColors[ 6] :=  RGB(  63,   0,   0); { Brown }
  BasicColors[ 7] :=  RGB( 127, 127, 127); { Light gray }
  BasicColors[ 8] :=  RGB(  63,  63,  63); { Gray }
  BasicColors[ 9] :=  RGB(   0,   0, 255); { Blue }
  BasicColors[10] :=  RGB(   0, 255,   0); { Green }
  BasicColors[11] :=  RGB(   0, 255, 255); { Cyan }
  BasicColors[12] :=  RGB( 255,   0,   0); { Red }
  BasicColors[13] :=  RGB( 255,   0, 255); { Magenta }
  BasicColors[14] :=  RGB( 255, 255,   0); { Yellow }
  BasicColors[15] :=  RGB( 255, 255, 255); { White }

  FillStyles[0] :=  0;  {Uses background color      }
  FillStyles[1] :=  1;  {Uses draw color            }
  FillStyles[2] :=  0;  {--- fill                   } (* 0 = hs_horizontal *)
  FillStyles[3] :=  3;  {/// fill                   } (* 3 = hs_bdiagonal *)
  FillStyles[4] :=  3;  {/// thick fill             } (* 3 = hs_bdiagonal *)
  FillStyles[5] :=  2;  {\\\ thick fill             } (* 2 = hs_fdiagonal *)
  FillStyles[6] :=  2;  {\\\ fill                   } (* 2 = hs_fdiagonal *)
  FillStyles[7] :=  4;  {Light hatch fill           } (* 4 = hs_cross *)
  FillStyles[8] :=  4;  {Heavy cross hatch          } (* 4 = hs_cross *)
  FillStyles[9] :=  5;  {Interleaving line          } (* 5 = hs_diagcross *)
  FillStyles[10] := 0;  {Widely spaced dot          } (* 0 = hs_horizontal *)
  FillStyles[11] := 0;  {Closely spaced dot         } (* 0 = hs_horizontal *)
  FillStyles[12] := 0;  {User-defined fill          }

END;

PROCEDURE TRT. RTFreeWindow(VAR w: rtstatpntr);
VAR i: INTEGER;
BEGIN
  IF (w <> NIL) THEN
  BEGIN
    Dispose(w);
    w := NIL;
  END;
END;

PROCEDURE TRT. RTClearWindowData(VAR w: rtstatpntr);
VAR i: INTEGER;
BEGIN
  RTFreeWindow(w);
  NEW(w);
END;

{Ancien RTCloseGraphics }
Destructor TRT.Done;
VAR i,j: INTEGER;
    freewin : Integer;
BEGIN
  CloseGraph;
  freewin := 1;
  IF freewin = 1 THEN
    FOR i := 0 TO RTNumWin-1 do RTFreeWindow(rtstat^[i]);
  FreeMem(rtstat, SizeOf(rtstatpntr) * MaxWin);
  if WinFont <> 0 then DeleteObject(WinFont);
  TObject.Done;
END;

END.
